1

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

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);
}

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

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 版本。

什么可能导致问题?先感谢您!

4

2 回答 2

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);
  }

所以现在要替换一个注释,你必须使用这个方法:

replaceAnnotation(index, "New Text", newX, newY);
于 2013-07-24T01:34:01.947 回答
0

XYSeries在通过 snv 获取 achartengine 之后,我认为删除注释并添加新的原因异常可能是因为在 它看起来是错误之前 添加 了clear调用。clearAnnotations()public synchronized void clearAnnotations() { mStringXY.clear(); }mAnnotations.clear();mStringXY.clear();

于 2014-02-09T14:03:51.920 回答