我正在编写一个代码,其中必须通过重复调用 aSyncTask 来不断更新图形。但为了确保图表不会太压缩,我计划删除/删除超过某个点的最旧条目。目前我的代码如下:
int counter = 0;
private class UpdateLineGraphAsync extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// add the next series value
series.add(counter, Math.sin((double) counter / 100));
counter += 20;
if (counter > 500)
series.remove(0);
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
((GraphicalView) lineGraph).repaint(); // repaints the graph!
}
}
但是当我调用 remove(0); 时,这段代码一直给我一个空点异常;谁能解释函数 XYSeries.remove(index) 的工作原理?我在某处读到它是一个后进先出结构。这是真的?如何纠正这个错误?即使是对 remove 函数的一次调用也会产生错误。
错误代码是:
07-03 17:27:36.080: E/AndroidRuntime(1229): FATAL EXCEPTION: AsyncTask #2
07-03 17:27:36.080: E/AndroidRuntime(1229): java.lang.RuntimeException: An error occured while executing doInBackground()
07-03 17:27:36.080: E/AndroidRuntime(1229): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.lang.Thread.run(Thread.java:1027)
07-03 17:27:36.080: E/AndroidRuntime(1229): Caused by: java.lang.NullPointerException
07-03 17:27:36.080: E/AndroidRuntime(1229): at org.achartengine.model.XYSeries.getY(XYSeries.java:169)
07-03 17:27:36.080: E/AndroidRuntime(1229): at org.achartengine.model.XYSeries.initRange(XYSeries.java:83)
07-03 17:27:36.080: E/AndroidRuntime(1229): at org.achartengine.model.XYSeries.remove(XYSeries.java:140)
07-03 17:27:36.080: E/AndroidRuntime(1229): at com.clearbridgevitalsigns.cardioleaf.LiveView$UpdateLineGraphAsync.doInBackground(LiveView.java:121)
07-03 17:27:36.080: E/AndroidRuntime(1229): at com.clearbridgevitalsigns.cardioleaf.LiveView$UpdateLineGraphAsync.doInBackground(LiveView.java:1)
07-03 17:27:36.080: E/AndroidRuntime(1229): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-03 17:27:36.080: E/AndroidRuntime(1229): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-03 17:27:36.080: E/AndroidRuntime(1229): ... 4 more