Using MPChartLibrary Inside a Fragment

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

I try to implement it in a fragment, but I get an error message and cannot understand 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 a layout of my activity

 <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 class Fragment

 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> 

Currently crashing while trying to increase the view in my OnCreateView function.

I get an error:

java.lang.RuntimeException: Unable to trigger ComponentInfo action {com.MyCompany.ChartTest / com.MyCompany.ChartTest.MainActivity}: android.view.InflateException: line of binary XML file # 5: inflation of class com.github.mikephil.charting inflated. charts.LineChart

+5
source share
5 answers

I was able to confirm where the problem is.

I have a theme for my applications defined in a library that is used by my other applications, but for some reason, referring to a theme from the library when using MPAndroidChartLib, I get this exception. If I copy and paste the theme style from my Library, and this is in my own application styles file and reference it, then it works fine.

+2
source

I honestly don’t see anything that could be wrong.

The only difference I see between the Fragments used in the wrap_content 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> 
+1
source

Try it, this may help:

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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> 

Line:

 xmlns:app="http://schemas.android.com/apk/res-auto" 

.

Hope this helps.

0
source

I struggled with this exact problem and finally found a working solution.

The key was an error message in the preview of the diagram fragment:

The following classes cannot be created:
charting.charts.LineChart

Exception Details
java.lang.ClassNotFoundException:
com.nineoldandroids.animation.ValueAnimator $ AnimatorUpdateListener

Adding com.nineoldandroids:library:2.4.+ the build.gradle project file fixed the problem:

 dependencies { compile 'com.android.support:support-v4:19.1.0' compile 'joda-time:joda-time:2.5' compile 'com.nineoldandroids:library:2.4.+' compile files('libs/MPChart.jar') } 
0
source

Adding the lines below in onCreate () helped

 getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
0
source

Source: https://habr.com/ru/post/1209016/


All Articles