0

我正在尝试将一些 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;

为什么会发生此错误,我在代码中做错了什么?

4

1 回答 1

2

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

于 2012-10-30T10:11:56.133 回答