android maps:数组索引超出范围的异常

前端之家收集整理的这篇文章主要介绍了android maps:数组索引超出范围的异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个任务,要显示Android地图中显示的458个标记.为避免与性能相关的问题,我使用AsyncTask实例更新了地图上的数据.

这是我做的一个简短场景.

>我取得英国各地458个地点的纬度/经度.
>我运行循环,根据Android博客教程,我将它们添加到ItemizedOverlay类中
>每50次迭代后,我调用publishProgress方法在地图中放置50个标记.

在第50次迭代之后,流程通过publishProgress进入onProgressUpdate,这是我的onProgressUpdate方法代码

// MapOverLays = mapView.getOverlays(); 
//This line was called in asyc task's constructor   
// Hello Overlay is an instance of ItemizedOverlay.
mapOverlays.add(helloOverLay);
//MapView.getController - Also called in Constructor
controller.setZoom(12);

controller.animateTo(centerPoint);
controller.setCenter(centerPoint);

代码抛出ArrayIndexOutOfBoundException,并且logcat不显示我的模块中的任何类.如果它解释了我的问题,这是logcat转储.

12-07 11:34:48.644: ERROR/AndroidRuntime(508): java.lang.ArrayIndexOutOfBoundsException
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:211)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.Overlay.draw(Overlay.java:179)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.MapView.onDraw(MapView.java:476)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.View.draw(View.java:6535)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.View.draw(View.java:6538)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.View.draw(View.java:6538)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.os.Looper.loop(Looper.java:123)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at java.lang.reflect.Method.invoke(Method.java:521)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at dalvik.system.NativeStart.main(Native Method)

附:我已经使用相对较小(10)和相对较大(150)的迭代而不是50来测试应用程序.但应用程序抛出相同的错误.

解决方法

我有同样的问题.看起来它是由更新ItemizedOverlay中的数据而不事后调用populate引起的.
public class ListOverlay extends ItemizedOverlay<OverlayItem> {
...
  public synchronized void updateLocations(List<OverlayItem> newLocations) {
    locations.clear();
    locations.addAll(newLocations);
    populate(); // this was missing
  }
  protected synchronized OverlayItem createItem(int index) {
    return locations.get(index);
  }
  public synchronized int size() {
    return locations.size();
  }
}
原文链接:/android/314948.html

猜你在找的Android相关文章