Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update MapView overlay items from an AsyncTask without ArrayIndexOutOfBoundsException
    primarykey
    data
    text
    <p>I am using a MapView which needs a number of overlay items added to it. I'm finding adding the overlay items is slow and causes the map to ANR when dragged. So I built an AsyncTask to add the overlay items. Initially I found it failed consistently as I was accessing the overlay collection from a background thread, and I gather it is not thread safe. So I changed it so the overlays are only changed from the UI thread. It now works, but only most of the time. It still crashes very occasionally when the map is touched.</p> <p>Here's the AsyncTask (an inner class inside my subclass of MapView):</p> <pre><code>class showItemsTask extends AsyncTask&lt;Void, User, Void&gt; { public boolean stop = false; @Override protected void onPreExecute() { super.onPreExecute(); } protected Void doInBackground(Void... v) { User[] items = Item.getItemList(); if (items != null &amp;&amp; items.length &gt; 0) { int i = 0; for (User item : items) { publishProgress(item); i++; if (stop || i&gt;MAX_NUMBER_OF_ITEMS) break; } stop = false; } return null; } @Override protected void onProgressUpdate(User... itemContainer) { super.onProgressUpdate(itemContainer); User item = itemContainer[0]; showItem(item.location.latitude, item.location.longitude, item.location.firstname, ((Integer) item.location.id).toString()); } public void showItem(float latitude, float longitude, String itemTitle, String itemSubtitle) { try { GeoPoint point = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000)); OverlayItem marker = new OverlayItem(point, itemTitle, itemSubtitle); availableItemsOverlay.addOverlay(marker); } catch (Exception e) { Trace.e(TAG, "Exception drawing a item"); } } protected void onPostExecute(Void v) { invalidate(); } } </code></pre> <p>Here's the stack trace:</p> <pre><code>0 java.lang.ArrayIndexOutOfBoundsException 1 at com.google.android.maps.ItemizedOverlay.maskHelper(ItemizedOverlay.java:562) 2 at com.google.android.maps.ItemizedOverlay.setFocus(ItemizedOverlay.java:365) 3 at com.google.android.maps.ItemizedOverlay.focus(ItemizedOverlay.java:539) 4 at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:455) 5 at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83) </code></pre> <p>Am I going down the wrong path with AsyncTask? If not, can you see why I'm getting this exception, when all changes to overlays are being made in the UI thread?</p>
    singulars
    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.
 

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