几天来,我一直在努力解决以下问题。我发现了很多关于这个主题的主题,但都有些不同,或者没有解决方案。
对于我的项目,我创建了一个自定义ItemizedOverlay
并将其添加到我的MapView
. 如果我现在删除项目列表的最后一项,我会得到一个IndexOutOfBoundsException
声称请求的索引等于ArrayList
. 即索引 2 大小 2 或索引 0 大小 0。从其他主题告诉我的内容中,我已经尝试了populate()
andsetLastFocusedIndex(-1)
方法。这些解决了我遇到的其他问题,但没有解决这个问题。从列表中删除其他项目时,它工作正常,问题似乎只发生在最后一项。
我得到以下 Logcat 输出:
01-24 16:11:08.091: E/AndroidRuntime(916): Uncaught handler: thread main exiting due to uncaught exception
01-24 16:11:08.101: E/AndroidRuntime(916): java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
01-24 16:11:08.101: E/AndroidRuntime(916): at java.util.ArrayList.get(ArrayList.java:341)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.google.android.maps.ItemizedOverlay.getItem(ItemizedOverlay.java:419)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.google.android.maps.ItemizedOverlay.focus(ItemizedOverlay.java:538)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:455)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:346)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:506)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.google.android.maps.MapView.onTouchEvent(MapView.java:628)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.View.dispatchTouchEvent(View.java:3709)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.os.Looper.loop(Looper.java:123)
01-24 16:11:08.101: E/AndroidRuntime(916): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-24 16:11:08.101: E/AndroidRuntime(916): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:11:08.101: E/AndroidRuntime(916): at java.lang.reflect.Method.invoke(Method.java:521)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-24 16:11:08.101: E/AndroidRuntime(916): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-24 16:11:08.101: E/AndroidRuntime(916): at dalvik.system.NativeStart.main(Native Method)
令我困扰的是,它似乎是从标准调用方法ItemizedOverlay
。除了在构造函数中,我没有添加普通ItemizedOverlay
方法,也没有在我的方法中调用超级方法。itemizedOverlay
错误似乎仍然发生在 normal中,因此将 the为空是ItemizedOverlay
有意义的。ArrayList
我希望有人能指出我正确的方向,因为我真的觉得被困在这里。提前致谢!
这是我的代码:
public class GameItemOverlay extends ItemizedOverlay<Item> {
private ArrayList<Item> mOverlays = new ArrayList<Item>();
public GameItemOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
setLastFocusedIndex(-1);
populate();
}
public void itemDataReady(){
mOverlays = GameSession.items;
setLastFocusedIndex(-1);
populate();
}
@Override
protected Item createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int i){
GameSession.remove(mOverlays.get(i).getID()); //Removes the item according to it's position
setLastFocusedIndex(-1);
populate();
return true;
}
}