使用MPAndroidChart在PieChartActivity中按下后退时出现空指针异常

时间:2016-12-22 06:44:59

标签: android nullpointerexception pie-chart mpandroidchart

我正在从UserList活动中调用PieChartActivity。 PieChartActivity显示正常,但当我按下时,显示上一个活动(UserList活动),然后应用程序崩溃。

以下是我对PiceChartActivity的意图:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {


        case android.R.id.home:

            onBackPressed();
            return true;

        case R.id.action_piechart:

            Intent intentPiechart = new Intent(Userlist.this, PieChartActivity.class);
            startActivity(intentPiechart);

            return true;
        case R.id.action_logout:
            session.setLoggedin(false);
            Intent intentMain = new Intent(Userlist.this, MainActivity.class);
            startActivity(intentMain);
            finish();
            return true;
        // action with ID action_settings was selected




        default:
            return super.onOptionsItemSelected(item);
    }

这是我的PieChartActivity:

package com.example.naseera.userlogin;
import android.content.Context;
import android.content.Intent;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;



public class PieChartActivity extends AppCompatActivity{
    private float[] yData;
    //private float[] yData2 = {25.0f, 10.0f,66.0f};
    private String[] xData = {"Admin", "Standard User","Premium User"};
    PieChart pieChart;
    private DBHelper db;
    public Toolbar tb;
    private static String TAG = ".PieChartActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_piechart);
    tb = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(tb);

    if (getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    TypedValue typedValue = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
    int color = typedValue.data;





    pieChart = (PieChart) findViewById(R.id.idPieChart);
  //  pieChart.setDescription("User Types");
    pieChart.setRotationEnabled(true);
    pieChart.setHoleRadius(30f);
    pieChart.setTransparentCircleRadius(35f);
    pieChart.setTransparentCircleAlpha(0);

    pieChart.setCenterText("Registered Users");
    pieChart.setCenterTextSize(20);
    pieChart.setCenterTextColor(color);
    pieChart.setDrawEntryLabels(true);
    pieChart.setEntryLabelTextSize(20);
    pieChart.setTransparentCircleRadius(61f);
    pieChart.setNoDataTextColor(Color.RED);


    addDataSet();
    pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, Highlight h) {

            if (e == null)
                return;
            Log.i("VAL SELECTED",
                    "Value: " + e.getY() + ", index: " + h.getX()
                            + ", DataSet index: " + h.getDataSetIndex());

            String usertype = xData[Math.round(h.getX())];
            System.out.println(usertype);
            int numberofusers = Math.round(e.getY());
            System.out.println(numberofusers);


            Toast.makeText(PieChartActivity.this, usertype +" = " + numberofusers, Toast.LENGTH_SHORT).show();
  ;         }

        @Override
        public void onNothingSelected() {

        }
    });
}

private void addDataSet() {

    db = new DBHelper(this);
    yData = db.getNumberOfUsers();
    ArrayList<PieEntry> entries = new ArrayList<>();

    entries.add(new PieEntry(Math.round(yData[0]),"Admin"));
    entries.add(new PieEntry(Math.round(yData[1]),"Standard User"));
    entries.add(new PieEntry(Math.round(yData[2]), "Premium User"));


    TypedValue typedValue = new TypedValue();
    getTheme().resolveAttribute(R.color.green, typedValue, true);
    int greencolor = typedValue.data;


    PieDataSet pieDataSet = new PieDataSet(entries, "Registered Users");

    pieDataSet.setSliceSpace(2);
    pieDataSet.setValueTextSize(20);
    pieDataSet.setColor(Color.WHITE);
    pieDataSet.setValueTextColor(Color.WHITE);

    //add colors to dataset

    ArrayList<Integer> colors = new ArrayList<>();
    colors.add(ContextCompat.getColor(this, R.color.deeporange));

    colors.add(ContextCompat.getColor(this, R.color.teal));

    colors.add(ContextCompat.getColor(this, R.color.purple));
    pieDataSet.setColors(colors);


    //add legend to chart

    Legend legend = pieChart.getLegend();
    legend.setForm(Legend.LegendForm.CIRCLE);
    legend.setFormSize(15);
    legend.setTextSize(15);
    //legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);

    Legend l = pieChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);

    //create pie data object
    PieData pieData = new PieData(pieDataSet);
    pieChart.setData(pieData);
    pieChart.invalidate();

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {


        case android.R.id.home:

            onBackPressed();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

}


@Override
public void onBackPressed(){
    super.onBackPressed();
}
}

}

这是logcat:

12-22 12:33:51.996 76-98/? I/ActivityManager: Displayed com.example.naseera.userlogin/.PieChartActivity: +10s654ms
12-22 12:33:56.581 745-745/? D/AndroidRuntime: Shutting down VM
12-22 12:33:56.581 745-745/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40014760)
12-22 12:33:56.841 745-745/? E/AndroidRuntime: FATAL EXCEPTION: main
12-22 12:33:56.841 745-745/? E/AndroidRuntime: java.lang.NullPointerException
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.graphics.Canvas.setBitmap(Canvas.java:177)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at com.github.mikephil.charting.renderer.PieChartRenderer.releaseBitmap(PieChartRenderer.java:985)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at com.github.mikephil.charting.charts.PieChart.onDetachedFromWindow(PieChart.java:726)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.View.dispatchDetachedFromWindow(View.java:8197)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1968)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1887)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewRoot.doDie(ViewRoot.java:3191)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.ViewRoot.die(ViewRoot.java:3159)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:222)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:538)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2912)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.app.ActivityThread.access$2100(ActivityThread.java:122)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1035)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:132)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4025)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:491)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-22 12:33:56.841 745-745/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-22 12:33:56.881 76-76/? W/ActivityManager:   Force finishing activity com.example.naseera.userlogin/.Userlist
12-22 12:33:57.661 76-85/? W/ActivityManager: Activity pause timeout for ActivityRecord{40922448 com.example.naseera.userlogin/.Userlist}
12-22 12:34:01.601 745-745/? I/Process: Sending signal. PID: 745 SIG: 9

之前的活动显示一秒钟,然后应用程序崩溃。任何帮助都会很棒。感谢。

下面是logcat中显示的代码区域。

Canvas.java

 public void setBitmap(@Nullable Bitmap bitmap) {
        if (isHardwareAccelerated()) {
            throw new RuntimeException("Can't set a bitmap device on a HW accelerated canvas");
        }

        if (bitmap == null) {
            native_setBitmap(mNativeCanvasWrapper, null);
            mDensity = Bitmap.DENSITY_NONE;
        } else {
            if (!bitmap.isMutable()) {
                throw new IllegalStateException();
            }
            throwIfCannotDraw(bitmap);

            native_setBitmap(mNativeCanvasWrapper, bitmap);
            mDensity = bitmap.mDensity;
        }

        mBitmap = bitmap;
    }

PieChartRenderer.java

 public void releaseBitmap() {
        if (mBitmapCanvas != null) {
            mBitmapCanvas.setBitmap(null); /*points to this line*/
            mBitmapCanvas = null;
        }
        if (mDrawBitmap != null) {
            mDrawBitmap.get().recycle();
            mDrawBitmap.clear();
            mDrawBitmap = null;
        }
    }

PieChart.java

@Override
    protected void onDetachedFromWindow() {
        // releases the bitmap in the renderer to avoid oom error
        if (mRenderer != null && mRenderer instanceof PieChartRenderer) {
            ((PieChartRenderer) mRenderer).releaseBitmap();
        }
        super.onDetachedFromWindow();
    }

2 个答案:

答案 0 :(得分:0)

chosen-select

用此替换您的背压方法。

答案 1 :(得分:0)

我正在使用api 12的AVD,当文档中明确提到最小sdk是16时,我很方便忘了阅读。傻我。

代码在AVD上运行完美,api> = 16.感谢所有试图提供帮助的人。 :)