2011-02-04 77 views
0

我正在開發一個Android應用程序,用戶可以在屏幕上添加標記點擊; 它的工作原理,但我想通過點擊刪除其中一個標記時遇到問題;這裏是我的代碼(我跟着NooYawk的例子):無法刪除標記onTap

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

@ user525105:您沒有提出任何問題。 – CommonsWare 2011-02-04 18:04:07

+0

@CommonsWare:我編輯了我的問題 – havanakoda 2011-02-04 20:42:53

回答

0

我會擺脫鏈的超類(super.onTap(point, view)),除非有特別的東西,你期待的超換做您。這就是你的異常來自的地方 - 超類認爲數組中的項目比目前多。