2013-05-17 71 views
0

我想擺脫那個錯誤,但我沒有看到任何錯誤(對我來說); 有人吃出問題太多,但它給予了佈局的壞ID:addView causes NullPointerExceptionAchartEngine - 在佈局上的AddView導致NullPointerException

我參加了一個簡單的代碼示例從http://www.javaadvent.com/2012/12/achartengine-charting-library-for.html

public class LineGraph extends Activity { 

    private GraphicalView mChart; 
    private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset(); 
    private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 
    private XYSeries mCurrentSeries; 
    private XYSeriesRenderer mCurrentRenderer; 

    private void initChart() { 
     mCurrentSeries = new XYSeries("Sample Data"); 
     mDataset.addSeries(mCurrentSeries); 
     mCurrentRenderer = new XYSeriesRenderer(); 
     mRenderer.addSeriesRenderer(mCurrentRenderer); 
    } 

    private void addSampleData() { 
     mCurrentSeries.add(1, 2); 
     mCurrentSeries.add(2, 3); 
     mCurrentSeries.add(3, 2); 
     mCurrentSeries.add(4, 5); 
     mCurrentSeries.add(5, 4); 
    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.graph); 
    } 

    protected void onResume() { 
     super.onResume(); 
     if (mChart == null) { 
      LinearLayout layout = (LinearLayout) findViewById(R.id.chart); 
      initChart(); 
      addSampleData(); 
      mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f); 

     // Here is the problem => layout.addView(mChart); 

     } else { 
      mChart.repaint(); 
     } 
    } 
} 

和我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/chart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

    </LinearLayout> 

</LinearLayout> 

五月是我錯過了一個明顯的錯誤,但addView()給我一個nullPointerException,請有人可以告訴我我做錯了什麼?

編輯:和堆棧跟蹤

05-17 14:37:05.830: E/SensorManager(6703): thread start 
05-17 14:37:06.885: E/SpannableStringBuilder(6703): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
05-17 14:37:06.885: E/SpannableStringBuilder(6703): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
05-17 14:37:28.800: E/AndroidRuntime(6703): FATAL EXCEPTION: main 
05-17 14:37:28.800: E/AndroidRuntime(6703): java.lang.RuntimeException: Unable to resume activity {com.smartdroid/com.smartdroid.LineGraph}: java.lang.NullPointerException 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread.access$600(ActivityThread.java:140) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.os.Looper.loop(Looper.java:137) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread.main(ActivityThread.java:4898) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at dalvik.system.NativeStart.main(Native Method) 
05-17 14:37:28.800: E/AndroidRuntime(6703): Caused by: java.lang.NullPointerException 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at com.smartdroid.LineGraph.onResume(LineGraph.java:64) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1188) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.Activity.performResume(Activity.java:5280) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606) 
05-17 14:37:28.800: E/AndroidRuntime(6703):  ... 12 more 
05-17 14:38:45.465: E/SensorManager(8335): thread start 
05-17 14:38:46.630: E/SpannableStringBuilder(8335): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
05-17 14:38:46.630: E/SpannableStringBuilder(8335): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
+0

stacktrace from logcat please? –

+0

恐怕看起來很愚蠢,但是當我啓動我的應用程序時,你的意思是來自logcat的所有消息,對吧? – Jackyto

+0

不僅來自logcat的錯誤堆棧跟蹤。請清洗您的項目 –

回答

0

傢伙是我的錯:我從佈局中設置添加圖表佈局到其它XML,不同時的onCreate()被調用。 對不起所有那些爛攤子,謝謝你們的好意

相關問題