问题
I'm using ItemizedOverlay to display points of events on a map. My main goal is to be able to find an event that was added earlier so that I can delete (or edit) just it without clearing the whole itemizedOverlay. So I decided to add each event to the ArrayList using its own unique ID as an index.
This is the code where I add the event.
myItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem = new OverlayItem(geoPoint, title, subtext);
itemizedOverlay.addOverlay(overlayItem, event_id);
mapOverlays.add(itemizedOverlay);
This is the ArrayList and constructor code from myItemizedOverlay class:
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public void addOverlay(OverlayItem overlay, int event_id)
{
// **Original** mOverlays.add(overlay);
mOverlays.add(event_id, overlay);
populate();
}
So what I think I'm saying is: add the item OverlayItem 'overlay' to ArrayList mOverlays at position 'event_id' (say, 4).
This gives me an error though at the part:
mOverlays.add(event_id, overlay);
error:
05-18 13:17:03.989: E/AndroidRuntime(559): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
Does anyone know what's going wrong here? Anyway, I know that even if this works, I can't utilize it using ArrayList because upon deleting an event later on (therefore an item in ArrayList) the index of all other items will shift accordingly? Which will mess it up.
So I looked it up and found that I should use SortedMap instead:
private SortedMap<Integer, OverlayItem> mOverlays = new TreeMap<Integer, OverlayItem>();
So I did, and changed the following line of code to this:
mOverlays.put(event_id, overlay);
But this gave me errors I could not understand why (see full error below): Specifically, it gave me error at line 42 in myItemizedOverlay:
populate();
at line 311 of ItemizedOverlay, which I do not know how to access and see what that line includes, but it obviously has something to do with the populate() error just above.
and at line 418 where I had this
itemizedOverlay.addOverlay(overlayItem, event_id);
Full error:
05-18 13:15:16.450: E/AndroidRuntime(524): FATAL EXCEPTION: main
05-18 13:15:16.450: E/AndroidRuntime(524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cylbs.android/com.cylbs.android.Home}: java.lang.NullPointerException
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.os.Looper.loop(Looper.java:130)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-18 13:15:16.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 13:15:16.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invoke(Method.java:507)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-18 13:15:16.450: E/AndroidRuntime(524): at dalvik.system.NativeStart.main(Native Method)
05-18 13:15:16.450: E/AndroidRuntime(524): Caused by: java.lang.NullPointerException
05-18 13:15:16.450: E/AndroidRuntime(524): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:311)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.cylbs.android.MyItemizedOverlay.addOverlay(MyItemizedOverlay.java:42)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.cylbs.android.Home.getEvents(Home.java:418)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.cylbs.android.Home.onCreate(Home.java:128)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
回答1:
What does your createItem
method look like?
this should probably be a comment, but I don't have access to comments so I'll delete when I get a response
回答2:
The error
05-18 13:15:16.450: E/AndroidRuntime(524): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:311)
is caused because the function createItem(int i)
returns null
.
来源:https://stackoverflow.com/questions/10653938/adding-custom-index-to-arraylist-or-sortedmap-when-adding-overlayitem-to-itemize