我正在开发一款Android应用,用户可以在屏幕上添加标记; 它工作,但我有一个问题,当我想通过点击它删除其中一个标记;这是我的代码(我跟着NooYawk的例子):
private class PoiOverlay extends ItemizedOverlay<MyOverlayItem>{
private ArrayList<MyOverlayItem> mOverlays = new ArrayList<MyOverlayItem>();
public PoiOverlay() {
super(null);
populate();
// TODO Auto-generated constructor stub
}
@Override
protected boolean onTap(int index) {
Log.d("toccato",":"+index);
mOverlays.remove(index);
doPopulate();
return true;
}
public boolean onTap(GeoPoint point, MapView view) {
if (super.onTap(point, view))
return true;
int icon;
if (type==1)
icon=R.drawable.bluedot;
else
icon=R.drawable.reddot;
MyOverlayItem overlay=new MyOverlayItem(point, ""+id, ""+id, id, icon);
addOverlay(overlay);
doPopulate();
Poi poi=new Poi(id, point, type);
pois.addLast(poi);
inserted.addLast(id);
id+=1;
return true;
}
public void doPopulate(){
setLastFocusedIndex(-1);
populate();
}
public void addOverlay(MyOverlayItem overlay){
Drawable drawable=getMarker(overlay.markerInt);
overlay.marker=drawable;
mOverlays.add(overlay);
populate();
}
但它不起作用。我可以添加标记,但是当我尝试删除其中一个时,我得到一个超出范围的数组异常,我不知道为什么。
这是点击标记时的重复:
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): FATAL EXCEPTION: main
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at java.util.ArrayList.get(ArrayList.java:311)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.google.android.maps.ItemizedOverlay.getItem(ItemizedOverlay.java:419)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.google.android.maps.ItemizedOverlay.focus(ItemizedOverlay.java:538)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:455)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at offscreen.tagger.Main$PoiOverlay.onTap(Main.java:280)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at
com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.View.dispatchTouchEvent(View.java:3765)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:905)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.app.Activity.dispatchTouchEvent(Activity.java:2093)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.os.Looper.loop(Looper.java:144)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-04 22:17:29.418: ERROR/AndroidRuntime(9979): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我会删除超类(super.onTap(point, view)
)的链,除非你特别期待超类为你做的事情。这就是您的异常来自的地方 - 超类认为数组中的项目数量多于目前的数量。