MPChartAndroid 在 Kotlin 错误空对象引用中的 Fragment tabslider 上崩溃

MPChartAndroid Crash on Fragment tabslider in Kotlin Error null object reference

请帮助我尝试在 Fragment 上显示 MPChartAndroid 时崩溃。一切正常,没有碎片。

这是我的 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)

}

还有我的片段:

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

还有我的ActivityXML

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

这是我的片段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)

我认为这是因为您在 activity 而不是片段中设置了图表,因为我认为您可能希望在 viewpager 中的片段中显示图表(来自片段 xml)

因此您的 Statistik class 仅包含选项卡和 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)
}

然后将您的函数移动到您的片段并在 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)

}