我在使用 AChartEngine 库时遇到问题。在 XYSeries 的实例上调用 remove(index) 会产生空指针异常。这很奇怪,因为我知道该系列存在并且在崩溃之前显示了设定的计数。
public View addDataView(){
int datasetCount = dataset.getSeriesAt(0).getItemCount();
XYSeries seriesA = dataset.getSeriesAt(0);
XYSeries seriesB = dataset.getSeriesAt(1);
/** Enforce max data points on the displayed chart */
if (datasetCount > maximumDataPoints - 1) {
System.out.println("SizeOfSeries: "+seriesA.getItemCount());
seriesA.remove(0);
seriesB.remove(0);
}
//d.getTime() format: 1357120800000
seriesA.add(new Date().getTime(), 20 + new Random().nextInt() % 100); // PR // TODO
seriesB.add(new Date().getTime(), 20 + new Random().nextInt() % 100); // BO // TODO;
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesA);
dataset.addSeries(seriesB);
autoscroll();
return ChartFactory.getTimeChartView(context, dataset, renderer, "`ss h:mm a");
}
这是相关的 logcat 输出:
01-04 14:16:31.806: I/System.out(25215): SizeOfSeries: 20
01-04 14:16:31.806: D/AndroidRuntime(25215): Shutting down VM
01-04 14:16:31.806: W/dalvikvm(25215): threadid=1: thread exiting with uncaught exception (group=0x410fa300)
01-04 14:16:31.806: E/AndroidRuntime(25215): FATAL EXCEPTION: main
01-04 14:16:31.806: E/AndroidRuntime(25215): java.lang.NullPointerException
01-04 14:16:31.806: E/AndroidRuntime(25215): at org.achartengine.model.XYSeries.getY(XYSeries.java:169)
01-04 14:16:31.806: E/AndroidRuntime(25215): at org.achartengine.model.XYSeries.initRange(XYSeries.java:83)
01-04 14:16:31.806: E/AndroidRuntime(25215): at org.achartengine.model.XYSeries.remove(XYSeries.java:140)
01-04 14:16:31.806: E/AndroidRuntime(25215): at charts.Chart.addDataView(Chart.java:75)
01-04 14:16:31.806: E/AndroidRuntime(25215): at com.example.pml.PulseRateActivity$ChartThread.onProgressUpdate(PulseRateActivity.java:68)
01-04 14:16:31.806: E/AndroidRuntime(25215): at com.example.pml.PulseRateActivity$ChartThread.onProgressUpdate(PulseRateActivity.java:1)
01-04 14:16:31.806: E/AndroidRuntime(25215): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
01-04 14:16:31.806: E/AndroidRuntime(25215): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 14:16:31.806: E/AndroidRuntime(25215): at android.os.Looper.loop(Looper.java:137)
01-04 14:16:31.806: E/AndroidRuntime(25215): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-04 14:16:31.806: E/AndroidRuntime(25215): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 14:16:31.806: E/AndroidRuntime(25215): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 14:16:31.806: E/AndroidRuntime(25215): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-04 14:16:31.806: E/AndroidRuntime(25215): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-04 14:16:31.806: E/AndroidRuntime(25215): at dalvik.system.NativeStart.main(Native Method)