• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ArrayIndexOutOfBoundsException in ItemizedOverlay

idiot2k1

Lurker
Mar 30, 2010
8
0
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:

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​
 
I am not positive, but you have setLastFocusedIndex(-1); -1 is an index that is out of bounds of the array. This could be causing your problem. If it is indeed happening inside your draw function, that is really the only thing that could cause it... Try commenting out everything in the draw function, then go through, uncommenting each line, one at a time, trying to run between each pass of uncommenting. This will tell you exactly which line of code is causing the troubles.
 
Upvote 0
The reason I have the setLastFocusedIndex(-1) in there is due to conflicting code between when the service I have running reads and writes to the arraylist of arrays at the same time. If I add to the list in the middle of writing to the map it will throw the code off. If I pass -1 to setLastFocusedIndex it is supposed to alleviate the problem.

I am only going off of the post I found with similar issues at
https://sites.google.com/site/andro...arrayindexoutofbounds-from-my-itemizedoverlay
and
ItemizedOverlay ArrayIndexOutOfBoundsException - Android Developers | Google Groups

What I did was comment out the stuff after the try/catch block and it works fine, so the error is within the draw method, more specifically in the getIndexToDraw method of the itemizedOverlay:

Code:
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549): java.lang.ArrayIndexOutOfBoundsException
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.ItemizedOverlay[B].getIndexToDraw[/B](ItemizedOverlay.java:211)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.adamacdo.haultruckapp.HaulItemizedOverlay.draw(HaulItemizedOverlay.java:46)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.Overlay.draw(Overlay.java:179)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.MapView.onDraw(MapView.java:494)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.View.draw(View.java:6740)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.View.draw(View.java:6743)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.View.draw(View.java:6743)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewRoot.draw(ViewRoot.java:1407)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.os.Looper.loop(Looper.java:123)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at java.lang.reflect.Method.invoke(Method.java:521)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at dalvik.system.NativeStart.main(Native Method)


The app works with the try catch block, but I don't want to have to deal with this when I push it for my customer to use.

Thanks for any help with this unusual problem.
-Adam

PS I am going to try this tomorrow on my way to school to see if the app works sans the caught error, if anything it will be a step in the right direction.

PPS On an unrelated note, does Windows 7 automatically return while typing code for other people? Not a useful return, but right in the middle of what I am doing.

Thanks again.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones