2016-12-22 2 views
0

Ich rufe die PieChartActivity aus einer UserList-Aktivität. Die PieChartActivity wird zwar angezeigt, aber wenn ich zurück drücke, wird die vorherige Aktivität (UserList-Aktivität) angezeigt und die App stürzt ab.Null-Zeiger-Ausnahme, wenn Back in der PieChartActivity gedrückt wird - MPAndroidChart verwenden

Hier ist, wo ich Absicht PiceChartActivity nennen:

@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); 
    } 

Das ist mein PieChartActivity ist:

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(); 
} 
} 

} 

Dies ist die logcat ist:

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 

Der bisherige Tätigkeit Displays für einen Sekunde und dann stürzt die App ab. Jede Hilfe wird großartig sein. Vielen Dank.


Unten ist der Codebereich, der in logcat angezeigt wird.

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(); 
    } 
+0

haben Sie überprüfen, warum android.graphics.Canvas.setBitmap (Canvas.java:177) wird dies Null? –

+0

Könnte sein, dass Sie onBackPressed nicht so überschreiben sollten, wie Sie es taten. Es sieht seltsam aus. –

+0

Ich habe die OnBackPressed-Methode bearbeitet und auch das neue Logcat gepostet. –

Antwort

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

Ersetzen Sie Ihre Back-Verfahren mit dieser gedrückt.

+0

hinzugefügt ja ich habe das auch versucht. derselbe Fehler zeigt sich immer noch. –

0

Ich benutzte eine AVD von api 12, als es in der Dokumentation klar erwähnt wurde, dass die minimale sdk 16 war, die ich bequem zu lesen vergaß. Wie dumm von mir.

Der Code funktioniert perfekt auf AVD mit api> = 16. Danke an alle, die versucht haben zu helfen. :)

Verwandte Themen