我遇到了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)
答案 0 :(得分:0)
我有同样的问题。 最初问题来自initRange(),如果你删除一个最大/最小点。 但在评论initRange后,我仍然在行'double removedY = removedEntry.getValue();'上得到一个空指针of remove()。 它看起来像mXY.removeByIndex(index);返回一个值设置为null的元素,但我找不到原因。
我正在使用subversion上的最新版本。 它看起来像某种竞争条件,因为它不会总是在同一时间失败。我构建了一个“滑动窗口”循环,在该循环中我在图表上保持给定数量的常量点。在这个例子中它只是3,所以在第3点之后我每次添加一个我删除另一个。 首先,日志看起来很正常:
01-11 23:06:48.060: E/AChartGraph(2038): Added 1 item. getIndexForKey: 0
01-11 23:06:48.060: E/AChartGraph(2038): series1.getItemCount(): 1
01-11 23:06:48.060: E/AChartGraph(2038): Added 1 item. getIndexForKey: 1
01-11 23:06:48.060: E/AChartGraph(2038): series1.getItemCount(): 2
01-11 23:06:48.160: E/AChartGraph(2038): Added 1 item. getIndexForKey: 2
01-11 23:06:48.160: E/AChartGraph(2038): series1.getItemCount(): 3
01-11 23:06:48.160: E/AChartGraph(2038): Removed item 0. series1.getItemCount(): 2
01-11 23:06:48.160: E/AChartGraph(2038): Added 1 item. getIndexForKey: 2
01-11 23:06:48.160: E/AChartGraph(2038): series1.getItemCount(): 3
01-11 23:06:48.160: E/AChartGraph(2038): Removed item 0. series1.getItemCount(): 2
01-11 23:06:48.160: E/AChartGraph(2038): Added 1 item. getIndexForKey: 2
01-11 23:06:48.160: E/AChartGraph(2038): series1.getItemCount(): 3
01-11 23:06:48.160: E/AChartGraph(2038): Removed item 0. series1.getItemCount(): 2
01-11 23:06:48.160: E/AChartGraph(2038): Added 1 item. getIndexForKey: 2
01-11 23:06:48.160: E/AChartGraph(2038): series1.getItemCount(): 3
这种情况一直持续到这种情况发生:
01-11 23:06:55.300: E/AChartGraph(2038): Added 1 item. getIndexForKey: 2
01-11 23:06:55.300: E/AChartGraph(2038): series1.getItemCount(): 3
01-11 23:06:55.300: E/AChartGraph(2038): Removed item 0. series1.getItemCount(): 2
01-11 23:06:55.300: E/AChartGraph(2038): Added 1 item. getIndexForKey: 1
01-11 23:06:55.300: E/AChartGraph(2038): series1.getItemCount(): 2
01-11 23:06:55.300: E/AChartGraph(2038): Removed item 0. series1.getItemCount(): 1
01-11 23:06:55.300: E/AChartGraph(2038): Added 1 item. getIndexForKey: 2
01-11 23:06:55.300: E/AChartGraph(2038): series1.getItemCount(): 2
01-11 23:06:55.300: W/dalvikvm(2038): threadid=14: thread exiting with uncaught exception (group=0x40bfb930)
01-11 23:06:55.320: E/AndroidRuntime(2038): FATAL EXCEPTION: AsyncTask #4
01-11 23:06:55.320: E/AndroidRuntime(2038): java.lang.RuntimeException: An error occured while executing doInBackground()
01-11 23:06:55.320: E/AndroidRuntime(2038): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.lang.Thread.run(Thread.java:856)
01-11 23:06:55.320: E/AndroidRuntime(2038): Caused by: java.lang.NullPointerException
01-11 23:06:55.320: E/AndroidRuntime(2038): at org.achartengine.model.XYSeries.getY(XYSeries.java:169)
01-11 23:06:55.320: E/AndroidRuntime(2038): at org.achartengine.model.XYSeries.initRange(XYSeries.java:83)
01-11 23:06:55.320: E/AndroidRuntime(2038): at org.achartengine.model.XYSeries.remove(XYSeries.java:140)
01-11 23:06:55.320: E/AndroidRuntime(2038): at com.mbev.android.Tmty.Helpers.AChartGraph.addAndPrune(AChartGraph.java:226)
01-11 23:06:55.320: E/AndroidRuntime(2038): at com.mbev.android.Tmty.DashboardFragment$startUpdateTmtyThread.doInBackground(DashboardFragment.java:284)
01-11 23:06:55.320: E/AndroidRuntime(2038): at com.mbev.android.Tmty.DashboardFragment$startUpdateTmtyThread.doInBackground(DashboardFragment.java:1)
01-11 23:06:55.320: E/AndroidRuntime(2038): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-11 23:06:55.320: E/AndroidRuntime(2038): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-11 23:06:55.320: E/AndroidRuntime(2038): ... 3 more
请注意,添加点的索引突然变为1而不是2,而itemCount也会下降到2,我们只是丢了一个点! 如果我们用更多的点测试它需要更长的时间,我们会看到日志中我们逐渐开始“松散”点,直到它在XYSeries.getY上崩溃。我相信它正试图消除其中一个早先神秘消失的点。
编辑1: 在这种情况下,我正在绘制一个时间序列,当我使用相同的键(在这种情况下为日期)添加2个点时出现错误。 删除功能会移除给定键的所有点,但不确定它是否是预期的或是否存在错误。但它可能与您的错误相同。