1

Please Help i got Chrash when trying to display MPChartAndroid on Fragment. Everything is ok with no fragment.

this my activity :

class Statistik : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.statistik);

    //SliderTab Chart
    val adapterChartKu = AdapterChartKu(supportFragmentManager)
    adapterChartKu.addFragment(ClassPerTanggal(), "perTanggal")
    adapterChartKu.addFragment(ClassPerOngkir(), "perOngkir")
    adapterChartKu.addFragment(ClassPerJumlah(), "perJumlah")
    adapterChartKu.addFragment(ClassPerBulan(), "perBulan")
    viewChart.adapter = adapterChartKu
    tabsChart.setupWithViewPager(viewChart)

    setupBarChartData()

}

private fun setupBarChartData() {
    // create BarEntry for Bar Group
    val bargroup = ArrayList<BarEntry>()
    bargroup.add(BarEntry(0f, 30f, "0"))
    bargroup.add(BarEntry(1f, 2f, "1"))
    bargroup.add(BarEntry(2f, 4f, "2"))
    bargroup.add(BarEntry(3f, 6f, "3"))
    bargroup.add(BarEntry(4f, 8f, "4"))
    bargroup.add(BarEntry(5f, 10f, "5"))
    bargroup.add(BarEntry(6f, 22f, "6"))
    bargroup.add(BarEntry(7f, 12.5f, "7"))
    bargroup.add(BarEntry(8f, 22f, "8"))
    bargroup.add(BarEntry(9f, 32f, "9"))
    bargroup.add(BarEntry(10f, 54f, "10"))
    bargroup.add(BarEntry(11f, 28f, "11"))

    // creating dataset for Bar Group
    val barDataSet = BarDataSet(bargroup, "Test aja")

    barDataSet.color = ContextCompat.getColor(this, R.color.colorAccent)

    val data = BarData(barDataSet)

    barChart.setData(data)
    barCharts.setData(data)
    barChart.xAxis.position = XAxis.XAxisPosition.BOTTOM
    barChart.xAxis.labelCount = 11
    barChart.xAxis.enableGridDashedLine(5f, 5f, 0f)
    barChart.axisRight.enableGridDashedLine(5f, 5f, 0f)
    barChart.axisLeft.enableGridDashedLine(5f, 5f, 0f)
    barChart.description.isEnabled = false
    barChart.animateY(1000)
    barChart.legend.isEnabled = false
    barChart.setPinchZoom(true)
    barChart.data.setDrawValues(false)

}

And My Fragment :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Statistik">

<!-- TODO: Update blank fragment layout -->

    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/barCharts"
        android:layout_width="match_parent"
        android:layout_height="300dp">
    </com.github.mikephil.charting.charts.BarChart>

And My Activity XML

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CardBarChart"
        android:layout_margin="5dp"
        app:cardCornerRadius="6dp"
        app:cardElevation="5dp"
        app:layout_constraintTop_toBottomOf="@+id/GridTotalan">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabsChart"
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    app:tabGravity="fill"
                    app:tabIndicatorColor="@color/colorPrimary"
                    app:tabIndicatorHeight="2dp"
                    app:tabMode="fixed"
                    app:tabPaddingEnd="14sp"
                    app:tabPaddingStart="14dp"
                    app:tabSelectedTextColor="@color/colorPrimary"
                    app:tabTextAppearance="@style/TabKecil" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/viewChart"
                    android:layout_width="match_parent"
                    android:layout_height="350dp">

                </androidx.viewpager.widget.ViewPager>
            </LinearLayout>



    </androidx.cardview.widget.CardView>

THis MY Fragment xml

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CardBarChart"
        android:layout_margin="5dp"
        app:cardCornerRadius="6dp"
        app:cardElevation="5dp"
        app:layout_constraintTop_toBottomOf="@+id/GridTotalan">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabsChart"
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    app:tabGravity="fill"
                    app:tabIndicatorColor="@color/colorPrimary"
                    app:tabIndicatorHeight="2dp"
                    app:tabMode="fixed"
                    app:tabPaddingEnd="14sp"
                    app:tabPaddingStart="14dp"
                    app:tabSelectedTextColor="@color/colorPrimary"
                    app:tabTextAppearance="@style/TabKecil" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/viewChart"
                    android:layout_width="match_parent"
                    android:layout_height="350dp"
                    app:layout_constraintTop_toBottomOf="@+id/tabsChart">

                </androidx.viewpager.widget.ViewPager>
            </LinearLayout>


    </androidx.cardview.widget.CardView>

FATAL EXCEPTION: main Process: com.multiekspedisi.juarapaket, PID: 15237 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/cxxxom.xxx.xxx.Statistik}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.BarChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)

1 Answers1

0

I think its because you setup the chart in your activity instead in your fragment, because i think you may want to show the chart in fragment within viewpager (from your fragment xml)

So your Statistik class only contain the setup for tab and viewpager

class Statistik : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.statistik);

    //SliderTab Chart
    val adapterChartKu = AdapterChartKu(supportFragmentManager)
    adapterChartKu.addFragment(ClassPerTanggal(), "perTanggal")
    adapterChartKu.addFragment(ClassPerOngkir(), "perOngkir")
    adapterChartKu.addFragment(ClassPerJumlah(), "perJumlah")
    adapterChartKu.addFragment(ClassPerBulan(), "perBulan")
    viewChart.adapter = adapterChartKu
    tabsChart.setupWithViewPager(viewChart)
}

then move your function to your fragment and call it on onActivityCreated

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    setupBarChartData()
}

private fun setupBarChartData() {
    // create BarEntry for Bar Group
    val bargroup = ArrayList<BarEntry>()
    bargroup.add(BarEntry(0f, 30f, "0"))
    bargroup.add(BarEntry(1f, 2f, "1"))
    bargroup.add(BarEntry(2f, 4f, "2"))
    bargroup.add(BarEntry(3f, 6f, "3"))
    bargroup.add(BarEntry(4f, 8f, "4"))
    bargroup.add(BarEntry(5f, 10f, "5"))
    bargroup.add(BarEntry(6f, 22f, "6"))
    bargroup.add(BarEntry(7f, 12.5f, "7"))
    bargroup.add(BarEntry(8f, 22f, "8"))
    bargroup.add(BarEntry(9f, 32f, "9"))
    bargroup.add(BarEntry(10f, 54f, "10"))
    bargroup.add(BarEntry(11f, 28f, "11"))

    // creating dataset for Bar Group
    val barDataSet = BarDataSet(bargroup, "Test aja")

    barDataSet.color = ContextCompat.getColor(this, R.color.colorAccent)

    val data = BarData(barDataSet)

    barCharts.setData(data) // barCharts instead of barChart, by the id in your fragment
    barCharts.xAxis.position = XAxis.XAxisPosition.BOTTOM
    barCharts.xAxis.labelCount = 11
    barCharts.xAxis.enableGridDashedLine(5f, 5f, 0f)
    barCharts.axisRight.enableGridDashedLine(5f, 5f, 0f)
    barCharts.axisLeft.enableGridDashedLine(5f, 5f, 0f)
    barCharts.description.isEnabled = false
    barCharts.animateY(1000)
    barCharts.legend.isEnabled = false
    barCharts.setPinchZoom(true)
    barCharts.data.setDrawValues(false)

}
Erwin Kurniawan A
  • 958
  • 2
  • 9
  • 17