Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding custom index to ArrayList or SortedMap when adding overlayitem to ItemizedOverlay
    primarykey
    data
    text
    <p>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.</p> <p>This is the code where I add the event. </p> <pre><code>myItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable, mapView); OverlayItem overlayItem = new OverlayItem(geoPoint, title, subtext); itemizedOverlay.addOverlay(overlayItem, event_id); mapOverlays.add(itemizedOverlay); </code></pre> <p>This is the ArrayList and constructor code from myItemizedOverlay class:</p> <pre><code>private ArrayList&lt;OverlayItem&gt; mOverlays = new ArrayList&lt;OverlayItem&gt;(); public void addOverlay(OverlayItem overlay, int event_id) { // **Original** mOverlays.add(overlay); mOverlays.add(event_id, overlay); populate(); } </code></pre> <p>So what I think I'm saying is: add the item OverlayItem 'overlay' to ArrayList mOverlays at position 'event_id' (say, 4).</p> <p>This gives me an error though at the part:</p> <pre><code>mOverlays.add(event_id, overlay); </code></pre> <p>error:</p> <pre><code>05-18 13:17:03.989: E/AndroidRuntime(559): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0 </code></pre> <p>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.</p> <p>So I looked it up and found that I should use SortedMap instead:</p> <pre><code>private SortedMap&lt;Integer, OverlayItem&gt; mOverlays = new TreeMap&lt;Integer, OverlayItem&gt;(); </code></pre> <p>So I did, and changed the following line of code to this:</p> <pre><code>mOverlays.put(event_id, overlay); </code></pre> <p>But this gave me errors I could not understand why (see full error below): Specifically, it gave me error at line 42 in myItemizedOverlay:</p> <pre><code>populate(); </code></pre> <p>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.</p> <p>and at line 418 where I had this</p> <pre><code>itemizedOverlay.addOverlay(overlayItem, event_id); </code></pre> <p>Full error:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload