向ArrayList添加条目时出现ConcurrentModificationException

时间:2012-10-30 09:43:45

标签: android arraylist android-asynctask concurrentmodification

我正在尝试将一些MapOverlays添加到我的MapView中,我收到以下错误:

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)

这是我的AsyncTask的doInBackground()方法:

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;

为什么会出现此错误以及我的代码中出错了什么?

1 个答案:

答案 0 :(得分:2)

尝试同步mapOverlays.add(itemizedOverlay);。我认为这就是问题所在,多个线程试图同时添加。