0

I am trying to add some MapOverlays to my MapView and I get the following Error:

java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
at com.google.android.maps.MapView.onDraw(MapView.java:530)
at android.view.View.draw(View.java:13458)
...
at dalvik.system.NativeStart.main(Native Method)

This is my doInBackground() method of my AsyncTask:

Drawable marker = myContext.getResources().getDrawable(
                R.drawable.marker);
        Bitmap bitmap = ((BitmapDrawable) marker).getBitmap();
        Drawable marker_new = new BitmapDrawable(myContext.getResources(),
                Bitmap.createScaledBitmap(
                        bitmap, MainActivity.MARKER_SIZE * 10,
                        MainActivity.MARKER_SIZE * 10, true));

        mapOverlays = map.getOverlays();

        int minLat = Integer.MAX_VALUE;
        int maxLat = Integer.MIN_VALUE;
        int minLon = Integer.MAX_VALUE;
        int maxLon = Integer.MIN_VALUE;
        double fitFactor = 1.1;

        MainActivity.mapListAddress.clear();
        MainActivity.mapListTitle.clear();
        MainActivity.mapListLati.clear();
        MainActivity.mapListLongi.clear();
        MainActivity.parseMaps();

        for (int i = 0; i < MainActivity.mapListAddress.size(); i++) {
            // String pointAddress = MainActivity.mapListAddress.get(i);
            String pointTitel = MainActivity.mapListTitle.get(i);

            MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(
                    marker_new, map);

            double latitude = MainActivity.mapListLati.get(i) * 1e6;
            double longitude = MainActivity.mapListLongi.get(i) * 1e6;

            Geocoder geocoder;
            List<Address> addresses = null;
            geocoder = new Geocoder(Map.this, Locale.getDefault());
            try {
                addresses = geocoder.getFromLocation(MainActivity.mapListLati.get(i),
                        MainActivity.mapListLongi.get(i), 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            String pointAddress = address + "\n" + city + "\n" + country;

            GeoPoint p = new GeoPoint((int) latitude, (int) longitude);

            overlayitem = new OverlayItem(p, pointTitel,
                    pointAddress);

            itemizedOverlay.setBalloonBottomOffset(40);
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

            int lat = p.getLatitudeE6();
            int lon = p.getLongitudeE6();

            maxLat = Math.max(lat, maxLat);
            minLat = Math.min(lat, minLat);
            maxLon = Math.max(lon, maxLon);
            minLon = Math.min(lon, minLon);

            map.getController().zoomToSpan((int) (Math.abs(maxLat - minLat) * fitFactor),
                    (int) (Math.abs(maxLon - minLon) * fitFactor));
            map.getController().animateTo(new GeoPoint((maxLat + minLat) / 2,
                    (maxLon + minLon) / 2));
        }
        return null;

Why does this error occur and what am I doing wrong in my code?

3
  • stackoverflow.com/questions/9326129/… Commented Oct 30, 2012 at 9:49
  • Wow... That's actually what I've done. I googled it but wasn't able to find a solution which fits my problem. And I also tried some things with threads which in my case also didn't work... Commented Oct 30, 2012 at 9:54
  • Why the downvote? Who ever did this please explain. Lets not downvote correct and good questions please. Commented Nov 7, 2012 at 13:20

1 Answer 1

2

Try to synchronize the mapOverlays.add(itemizedOverlay);. I think this is where the issue is, multiple threads trying to add at the same time.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately this did not work for me. I just added synchronized to my doInBackground() method. Is that wrong?
Yes, that is wrong, synchronize(mapOverlays) { mapOverlays.add() ; } that should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.