android maps : array index out of bound exception
Asked Answered
H

5

7

I have a task to show 458 markers to show in Android maps. And to avoid performance related issues I update the data on map using an AsyncTask instance.

Here is a short scenario of what I do.

  1. I fetch the latitude/longitude of 458 locations around UK.
  2. I run the loop and as per Android Blog tutorial I add them in ItemizedOverlay class
  3. After every 50th iteration I call publishProgress method to place 50 markers in map.

After the 50th iteration the flow goes into onProgressUpdate via publishProgress and here is my code of onProgressUpdate method

// MapOverLays = mapView.getOverlays(); 
//This line was called in asyc task's constructor   
// Hello Overlay is an instance of ItemizedOverlay.
mapOverlays.add(helloOverLay);
//MapView.getController - Also called in Constructor
controller.setZoom(12);

controller.animateTo(centerPoint);
controller.setCenter(centerPoint);

This code throws ArrayIndexOutOfBoundException and the logcat doesn't show any of the class from my module. Here is the logcat dump if it elaborates my problem.

12-07 11:34:48.644: ERROR/AndroidRuntime(508): java.lang.ArrayIndexOutOfBoundsException
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:211)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.Overlay.draw(Overlay.java:179)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.google.android.maps.MapView.onDraw(MapView.java:476)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.View.draw(View.java:6535)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.View.draw(View.java:6538)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.View.draw(View.java:6538)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.os.Looper.loop(Looper.java:123)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at java.lang.reflect.Method.invoke(Method.java:521)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-07 11:34:48.644: ERROR/AndroidRuntime(508):     at dalvik.system.NativeStart.main(Native Method)

P.S. I have tested the application with relatively smaller (10) and relatively larger (150) iterations instead of 50. But the application throws same Error.

Hoptoad answered 7/12, 2010 at 6:56 Comment(1)
I have temporarily get the solution. Instead of adding pins frequently i first prepare whole data and than add it in map using onPostExecute() as described stackoverflow.com/questions/2870743 but it still doesn't meet my requirement to save the time to load all pins. However i can get rid of that exception. But waiting for any suggestions that improve the loading time.... Thanks anyway... :-)Hoptoad
H
0

I have temporarily get the solution. Instead of adding pins frequently i first prepare whole data and than add it in map using onPostExecute() as described Android 2.1 GoogleMaps ItemizedOverlay ConcurrentModificationException but it still doesn't meet my requirement to save the time to load all pins. However i can get rid of that exception. But waiting for any suggestions that improve the loading time.... Thanks anyway... :-)

Hoptoad answered 12/2, 2011 at 6:3 Comment(1)
I have the same problem with my mapview. I have yet to dive deep into the problem. Have you tried the answer above dealing with calling populate?Transcendent
W
3

I had the same problem. Looks like it was caused by updating the data in the ItemizedOverlay without calling populate afterwards.


public class ListOverlay extends ItemizedOverlay<OverlayItem> {
...
  public synchronized void updateLocations(List<OverlayItem> newLocations) {
    locations.clear();
    locations.addAll(newLocations);
    populate(); // this was missing
  }
  protected synchronized OverlayItem createItem(int index) {
    return locations.get(index);
  }
  public synchronized int size() {
    return locations.size();
  }
}

Waxy answered 21/12, 2010 at 15:26 Comment(0)
A
1

setLastFocusedIndex(-1); populate();

Not "always" work !!!

Aberrant answered 12/5, 2012 at 13:58 Comment(0)
H
0

I have temporarily get the solution. Instead of adding pins frequently i first prepare whole data and than add it in map using onPostExecute() as described Android 2.1 GoogleMaps ItemizedOverlay ConcurrentModificationException but it still doesn't meet my requirement to save the time to load all pins. However i can get rid of that exception. But waiting for any suggestions that improve the loading time.... Thanks anyway... :-)

Hoptoad answered 12/2, 2011 at 6:3 Comment(1)
I have the same problem with my mapview. I have yet to dive deep into the problem. Have you tried the answer above dealing with calling populate?Transcendent
T
0

I've had the same problem, only I get a slightly different stacktrace. I found the solution is to do the following to your itemizedOverlay object after you add your overlays:

setLastFocusedIndex(-1);
populate();

I can't take credit for this, I found the answer here. I don't know why you have to do this, but it is working for me.

Transcendent answered 5/3, 2012 at 22:9 Comment(0)
A
0

Simply call mapView's invalidate() in UI thread after new pin items are added to the overlay each time.

Aberrant answered 12/5, 2012 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.