Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MPChartLibrary within a fragment

I am working on an Android project and I am trying to implement a Library called MPAndroidChart from https://github.com/PhilJay/MPAndroidChart.

I am trying to implement it in a fragment but I keep getting an error and I cannot see why.

Below is my activity.

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment chartFragment = new ChartFragment();
        transaction.replace(R.id.fragmentContainer, chartFragment);
        transaction.commit();
    }
}

Below is my activities layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />
    <FrameLayout android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Below is my Fragment class

public class ChartFragment extends Fragment
{
    private LineChart mChart;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.graph, container, false);



        return v;
    }
}

And below is the layout for the fragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

At the moment it crashes when it tries to inflate the view in my OnCreateView function.

The error I am getting is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyCompany.ChartTest/com.MyCompany.ChartTest.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class com.github.mikephil.charting.charts.LineChart

like image 315
Boardy Avatar asked Oct 31 '22 12:10

Boardy


1 Answers

I honestly do not see anything that could be wrong.

The only difference I see between the Fragments used in the example project is that they use wrap_content instead of match_parent for the width and height of the chart.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>
like image 102
Philipp Jahoda Avatar answered Nov 12 '22 18:11

Philipp Jahoda