Hi there,
I have looked online at a few resources for this error that is happening, and I have seen some answers, but they don't seem to work and I am still getting this exception.
Basically I want to track the GPS location of the user's phone and draw points on the map to show where they have been. I am using the ItemizedOverlay tutorial from Google, but tweaked a little to do what I want to do. The ItemizedOverlay list is continually growing and I have a service running to add to the list on an interval. Every once in a while (when the list is being accessed to read and write at the same time) I get an arrayindexoutofboundsexception thrown. The workaround I have seen is to call setLastFocusedIndex(-1) before any populate() call. But I still get the error. Here is my code for the ItemizedOverlay class:
I have looked online at a few resources for this error that is happening, and I have seen some answers, but they don't seem to work and I am still getting this exception.
Basically I want to track the GPS location of the user's phone and draw points on the map to show where they have been. I am using the ItemizedOverlay tutorial from Google, but tweaked a little to do what I want to do. The ItemizedOverlay list is continually growing and I have a service running to add to the list on an interval. Every once in a while (when the list is being accessed to read and write at the same time) I get an arrayindexoutofboundsexception thrown. The workaround I have seen is to call setLastFocusedIndex(-1) before any populate() call. But I still get the error. Here is my code for the ItemizedOverlay class:
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]package [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]com.adamacdo.haultruckapp;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]java.util.ArrayList;[/SIZE] [/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][U]android.app.AlertDialog[/U];[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.content.Context;[/SIZE][/LEFT]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.graphics.Canvas;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.graphics.drawable.Drawable;[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.util.Log;[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]com.google.android.maps.ItemizedOverlay;[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]com.google.android.maps.MapView;[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]com.google.android.maps.OverlayItem;[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HaulItemizedOverlay [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ItemizedOverlay<OverlayItem> {[/SIZE][/LEFT]
[LEFT][SIZE=2]Context [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mContext[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Synchronize ArrayList access maybe?[/COLOR][/SIZE][/COLOR][/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<OverlayItem> [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<OverlayItem>();[/SIZE][/LEFT]
[/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HaulItemizedOverlay(Drawable defaultMarker) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([I]boundCenter[/I](defaultMarker));[/SIZE][/LEFT]
[LEFT][SIZE=2]setLastFocusedIndex(-1);[/SIZE]
[SIZE=2]populate();[/SIZE]
[LEFT][SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] addOverlay(OverlayItem overlay) {[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].add(overlay);[/SIZE]
[SIZE=2]setLastFocusedIndex(-1);[/SIZE]
[SIZE=2]populate();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]protected[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] OverlayItem createItem([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i) { [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].get(i);[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] size() {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].size();[/SIZE]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HaulItemizedOverlay(Drawable defaultMarker, Context context) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](defaultMarker);[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mContext[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = context;[/SIZE]
[SIZE=2]setLastFocusedIndex(-1);[/SIZE]
[SIZE=2]populate();[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/LEFT]
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] draw(Canvas canvas, MapView mapView, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]boolean[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] shadow) {[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](canvas!=[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] && mapView!=[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]){[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Exception thrown here[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].draw(canvas, mapView, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]false[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Exception e){[/SIZE]
[SIZE=2]Log.[I]d[/I]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"HaulItemizedOverlay"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Exception Thrown"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], e);[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]setLastFocusedIndex(-1);[/SIZE]
[SIZE=2]populate();[/SIZE]
[SIZE=2]mapView.invalidate();[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/LEFT]
[LEFT][SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] clear() {[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].clear();[/SIZE]
[SIZE=2]setLastFocusedIndex(-1);[/SIZE]
[SIZE=2]populate();[/SIZE][/LEFT]
[/LEFT]
[LEFT][SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
I have commented that the exception is thrown in the draw() method, but how would I not have it thrown? Is there another workaround / simple but of code I am missing?
Thanks for any help,
-Adam
-Adam