删除注释会在重绘时导致NullPointerException

时间:2013-07-23 22:50:33

标签: nullpointerexception achartengine

我正在使用achartengine,我在删除注释时会收到NullPointerException,然后再次使用不同的(x,y)值添加它。这是代码:

for(int n = 0; n < series.getAnnotationCount(); ++n){
    double x = toCoordinate(series.getAnnotationX(n)*prevTimeScale*1000, timeScale);
    double y = series.getAnnotationY(n);
    String text = series.getAnnotationAt(n);

    series.removeAnnotation(n);
    series.addAnnotation(n, text, x, y);
}

当前的ace框架中不存在带索引的addAnnotation()方法,但我添加了它:

public void addAnnotation(int index, String annotation, double x, double y) {
    mAnnotations.add(index, annotation);
    mStringXY.put(index, x, y);
}

基本上我删除给定索引处的注释,然后添加相同的文本,但使用不同的坐标。致电GraphicalView#repaint()后,我得到了NullPointerException

07-23 19:41:56.256: E/AndroidRuntime(17984): FATAL EXCEPTION: main
07-23 19:41:56.256: E/AndroidRuntime(17984): java.lang.NullPointerException
07-23 19:41:56.256: E/AndroidRuntime(17984):    at org.achartengine.model.XYSeries.getAnnotationY(XYSeries.java:294)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at org.achartengine.chart.XYChart.draw(XYChart.java:310)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at org.achartengine.GraphicalView.onDraw(GraphicalView.java:177)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.draw(View.java:13717)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.buildDrawingCache(View.java:12971)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12634)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.draw(View.java:13433)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12653)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12593)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12593)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12593)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12593)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12593)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.View.getDisplayList(View.java:12699)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.Choreographer.doFrame(Choreographer.java:532)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.os.Handler.handleCallback(Handler.java:725)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.os.Looper.loop(Looper.java:137)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at android.app.ActivityThread.main(ActivityThread.java:5233)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at java.lang.reflect.Method.invokeNative(Native Method)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at java.lang.reflect.Method.invoke(Method.java:511)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-23 19:41:56.256: E/AndroidRuntime(17984):    at dalvik.system.NativeStart.main(Native Method)
07-23 19:42:03.088: E/Trace(18186): error opening trace file: No such file or directory (2)

问题出在使用getAnnotationY(int index)中的getYByIndex(int index)的方法IndexXYMap中:

public V getYByIndex(int index) {
  K key = indexList.get(index);
  return this.get(key);
}

因为行return this.get(key)正在返回null,我不知道为什么,因为我正在使用索引添加注释。我正在使用SVN存储库中的最新ace版本。

可能导致问题的原因是什么?提前谢谢!

2 个答案:

答案 0 :(得分:1)

似乎使用当前API无法实现的功能。为此,我稍微修改了源代码。在IndexXYMap.java上添加此方法:

  public V set(int index, K key, V value){
    indexList.set(index, key);
    updateMaxXDifference();
    return super.put(key, value);
  }

这种方法可以在XYSeries.java替换注释:

  /**
   * Replaces an Annotation at the given index
   * 
   * @param index annotation index
   * @param annotation new String to put
   * @param x new x coordinate to use
   * @param y new y coordinate to use
   */
  public void replaceAnnotation(int index, String annotation, double x, double y){
    mAnnotations.set(index, annotation);
    mStringXY.set(index, x, y);
  }

现在要替换Annotation,你必须使用这个方法:

replaceAnnotation(index, "New Text", newX, newY);

答案 1 :(得分:0)

通过snv获取achartengine之后我认为删除注释并添加新原因异常可能是因为XYSeries的{​​{1}}调用clearclearAnnotations()public synchronized void clearAnnotations() { mStringXY.clear(); }之前添加mAnnotations.clear();这似乎是一个错误。