我试图摆脱那个错误,但我没有看到任何错误(对我来说);有人也遇到了这个问题,但这是给布局的错误 ID: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