MPAndroidChart不断抛出null错误

时间:2018-07-03 17:10:51

标签: java android mpandroidchart

我只是在尝试该库,以查看它是否符合我的需求,到目前为止,我还无法使它正常工作。这是代码

package com.example.user.bottomnavigationbar;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    PieChart pc;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    return true;

//                case R.id.navigation_overview:
//                    Intent intentOverview = new Intent(MainActivity.this, activity_test.class);
//                    intentOverview.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                    MainActivity.this.finish();
//                    startActivity(intentOverview);
//                    return true;

                case R.id.navigation_expenses:

                    return true;

                case R.id.navigation_reminder:

                    return true;

                case R.id.navigation_income:

                    return true;

                case R.id.navigation_login:
                    Intent intentLogin = new Intent(MainActivity.this, LoginActivity.class);
                    intentLogin.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    MainActivity.this.finish();
                    startActivity(intentLogin);
                    return true;
            }
            return false;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        pc = findViewById(R.id.chart);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        ArrayList <PieEntry> pieData = new ArrayList<>();
        pieData.add(new PieEntry(10));
        pieData.add(new PieEntry(20));
        pieData.add(new PieEntry(30));
        PieDataSet dataSet = new PieDataSet(pieData, "Survey Results");
        PieData data = new PieData(dataSet);
        pc.setData(data);
    }


    public void onBackPressed()
    {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Closing Activity")
                .setMessage("Are you sure you want to close this activity?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                })
                .setNegativeButton("No", null)
                .show();
    }
}

这是我得到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.user.bottomnavigationbar, PID: 4172
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.bottomnavigationbar/com.example.user.bottomnavigationbar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.PieChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.PieChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
                      at com.example.user.bottomnavigationbar.MainActivity.onCreate(MainActivity.java:78)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我搜索了一下,发现对别人来说,他们没有初始化对象,因此它引发了此错误。现在有人确实提到可以用代码创建图表,然后将其添加到活动中,如文档的“入门”页面所建议的那样,但我无法使其正常工作。那有什么呢?

P.S留在代码中进行导航,因为我不确定是否可能是问题的原因

1 个答案:

答案 0 :(得分:0)

您首先要找到该视图,然后将内容视图提供给您的活动,并替换为此代码

 @Override
        protected void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pc = findViewById(R.id.chart);
            BottomNavigationView navigation = findViewById(R.id.navigation);
            navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
            ArrayList <PieEntry> pieData = new ArrayList<>();
            pieData.add(new PieEntry(10));
            pieData.add(new PieEntry(20));
            pieData.add(new PieEntry(30));
            PieDataSet dataSet = new PieDataSet(pieData, "Survey Results");
            PieData data = new PieData(dataSet);
            pc.setData(data);
        }