1

我在使用 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)
4

1 回答 1

0

我也有同样的问题。最初,如果您删除了一个 Max/Min 点,则问题来自 initRange()。但是在评论 initRange 之后,我仍然在'double removedY = removedEntry.getValue();' 行上得到一个空指针 删除()。它看起来像 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 个点时出现错误。remove 函数会删除给定键的所有点,但不确定它是有意的还是错误的。但这很可能与您遇到的错误相同。

于 2013-01-11T12:46:28.323 回答