ArrayIndexOutOfBoundsException in ItemizedOverlay


Last Updated:

  1. idiot2k1

    idiot2k1 Member This Topic's Starter

    Joined:
    Mar 30, 2010
    Messages:
    8
    Likes Received:
    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 (Text):
    1.  
    2.  
    3. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]package [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]com.adamacdo.haultruckapp;[/SIZE]
    4.  
    5.  
    6. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]java.util.ArrayList;[/SIZE] [/LEFT]
    7.  
    8. [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]
    9.  
    10. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.content.Context;[/SIZE][/LEFT]
    11.  
    12. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.graphics.Canvas;[/SIZE]
    13.  
    14. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.graphics.drawable.Drawable;[/SIZE][/LEFT]
    15.  
    16. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.util.Log;[/SIZE][/LEFT]
    17.  
    18. [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]
    19.  
    20. [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]
    21.  
    22. [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]
    23.  
    24.  
    25.  
    26. [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]
    27.  
    28. [LEFT][SIZE=2]Context [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mContext[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    29.  
    30. [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Synchronize ArrayList access maybe?[/COLOR][/SIZE][/COLOR][/SIZE]
    31. [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]
    32. [/LEFT]
    33.  
    34.  
    35.  
    36.  
    37.  
    38. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HaulItemizedOverlay(Drawable defaultMarker) {[/SIZE]
    39.  
    40. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([I]boundCenter[/I](defaultMarker));[/SIZE][/LEFT]
    41.  
    42.  
    43.  
    44.  
    45.  
    46. [LEFT][SIZE=2]setLastFocusedIndex(-1);[/SIZE]
    47.  
    48. [SIZE=2]populate();[/SIZE]
    49. [LEFT][SIZE=2]}[/SIZE]
    50. [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]
    51. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].add(overlay);[/SIZE]
    52. [SIZE=2]setLastFocusedIndex(-1);[/SIZE]
    53. [SIZE=2]populate();[/SIZE]
    54. [SIZE=2]}[/SIZE]
    55. [SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
    56. [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]
    57. [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]
    58. [SIZE=2]}[/SIZE]
    59. [SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
    60. [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]
    61. [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]
    62. [SIZE=2]}[/SIZE]
    63. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HaulItemizedOverlay(Drawable defaultMarker, Context context) {[/SIZE]
    64. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](defaultMarker);[/SIZE]
    65. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mContext[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = context;[/SIZE]
    66. [SIZE=2]setLastFocusedIndex(-1);[/SIZE]
    67. [SIZE=2]populate();[/SIZE]
    68. [SIZE=2]}[/SIZE][/LEFT]
    69. [/LEFT]
    70.  
    71.  
    72.  
    73.  
    74.  
    75. [LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
    76.  
    77. [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]
    78. [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]
    79. [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Exception thrown here[/COLOR][/SIZE][/COLOR][/SIZE]
    80. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]{[/SIZE]
    81. [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]
    82. [SIZE=2]}[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Exception e){[/SIZE]
    83. [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]
    84. [SIZE=2]}[/SIZE]
    85. [SIZE=2]setLastFocusedIndex(-1);[/SIZE]
    86. [SIZE=2]populate();[/SIZE]
    87. [SIZE=2]mapView.invalidate();[/SIZE]
    88. [SIZE=2]}[/SIZE][/LEFT]
    89. [/LEFT]
    90.  
    91.  
    92.  
    93.  
    94.  
    95. [LEFT][SIZE=2]}[/SIZE]
    96.  
    97. [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]
    98. [LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mOverlays[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].clear();[/SIZE]
    99. [SIZE=2]setLastFocusedIndex(-1);[/SIZE]
    100. [SIZE=2]populate();[/SIZE][/LEFT]
    101. [/LEFT]
    102.  
    103.  
    104.  
    105.  
    106.  
    107. [LEFT][SIZE=2]}[/SIZE]
    108.  
    109. [SIZE=2]}[/SIZE][/LEFT]
    110.  
    111.  
    112.  
    113.  
    114.  
    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​
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     
  3. idiot2k1

    idiot2k1 Member This Topic's Starter

    Joined:
    Mar 30, 2010
    Messages:
    8
    Likes Received:
    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 (Text):
    1. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549): java.lang.ArrayIndexOutOfBoundsException
    2. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.ItemizedOverlay[B].getIndexToDraw[/B](ItemizedOverlay.java:211)
    3. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
    4. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.adamacdo.haultruckapp.HaulItemizedOverlay.draw(HaulItemizedOverlay.java:46)
    5. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.Overlay.draw(Overlay.java:179)
    6. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45)
    7. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.google.android.maps.MapView.onDraw(MapView.java:494)
    8. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.View.draw(View.java:6740)
    9. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
    10. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    11. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    12. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    13. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.View.draw(View.java:6743)
    14. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    15. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
    16. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    17. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.View.draw(View.java:6743)
    18. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    19. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
    20. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewRoot.draw(ViewRoot.java:1407)
    21. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
    22. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    23. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.os.Handler.dispatchMessage(Handler.java:99)
    24. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.os.Looper.loop(Looper.java:123)
    25. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    26. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at java.lang.reflect.Method.invokeNative(Native Method)
    27. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at java.lang.reflect.Method.invoke(Method.java:521)
    28. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    29. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    30. 10-14 00:22:46.488: ERROR/HaulItemizedOverlay(4549):     at dalvik.system.NativeStart.main(Native Method)
    31.  

    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.
     

Share This Page

Loading...