kokilayaa kokilayaa - 1 year ago 47
Android Question

App crashed when the Fragment that uses MP Android Chart library is executed

I'm using a Navigation drawer activity with fragments and I have implemented MP Android Chart library on one of those fragments. But when that fragment is executed, the app crashes. I went through so many threads and tutorial but I can't figure out where the problem is.

I have coded the following lines on respective classes and files.

The Fragment class:

package com.example.maleesha5.elektra11;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

/**
* Created by kokil on 21/05/2017.
*/


public class Statistics extends android.support.v4.app.Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.statistics, container, false);

BarChart chart = (BarChart) rootView.findViewById(R.id.chart);

BarData data = new BarData(getXAxisValues(), getDataSet());
chart.setData(data);
chart.setDescription("My Chart");
// chart.animateXY(2000, 2000);
chart.invalidate();

return rootView;
}

private ArrayList<BarDataSet> getDataSet() {
ArrayList<BarDataSet> dataSets = null;

ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(90.000f, 4); // May
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun
valueSet1.add(v1e6);

ArrayList<BarEntry> valueSet2 = new ArrayList<>();
BarEntry v2e1 = new BarEntry(150.000f, 0); // Jan
valueSet2.add(v2e1);
BarEntry v2e2 = new BarEntry(90.000f, 1); // Feb
valueSet2.add(v2e2);
BarEntry v2e3 = new BarEntry(120.000f, 2); // Mar
valueSet2.add(v2e3);
BarEntry v2e4 = new BarEntry(60.000f, 3); // Apr
valueSet2.add(v2e4);
BarEntry v2e5 = new BarEntry(20.000f, 4); // May
valueSet2.add(v2e5);
BarEntry v2e6 = new BarEntry(80.000f, 5); // Jun
valueSet2.add(v2e6);

BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
barDataSet1.setColor(Color.rgb(0, 155, 0));
BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
dataSets.add(barDataSet2);
return dataSets;
}

private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("JAN");
xAxis.add("FEB");
xAxis.add("MAR");
xAxis.add("APR");
xAxis.add("MAY");
xAxis.add("JUN");
return xAxis;
}
}


The Main Activity class:

} else if (id == R.id.nav_stats) {
setTitle("Statistics");
Statistics stats= new Statistics();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_main, stats, "Stats");
fragmentTransaction.commit();


The build.gradle file:

repositories {
maven { url "https://jitpack.io" }
}

dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.0.9'
}


The layout XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

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

</RelativeLayout>

Answer Source

Replace BarChart chart = (BarChart) rootView.findViewById(R.id.chart); with

LineChart chart = (LineChart) rootView.findViewById(R.id.chart);

In your xml you are using class com.github.mikephil.charting.charts.LineChart while in your fragment you are calling BarChart class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download