Android中的ConcurrentModificationException

时间:2011-05-22 21:25:52

标签: android itemizedoverlay concurrentmodification

首先,我很抱歉再问这个问题,但我还没有找到解决问题的方法。

我有一个应用程序,我可以使用MyLocationOverlay定位自己,并使用itemizedOverlay绘制朋友的位置。但该应用程序崩溃与ConcurrentModificationExcepion

对于我的位置,我使用MyLocationOverlay和这个单一代码(previusly initialized):

myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);

对于我的朋友'positión,我使用了ItemizedOverlay和一种绘制位置的方法。 ItemizedOverlay这是:

    package app.localizadroid.ACT;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.CopyOnWriteArrayList;

    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.widget.Toast;

    import com.google.android.maps.MapView;
    import com.google.android.maps.OverlayItem;

    public class Tab_Activity_map_HelloItemizedOverlay extends  Tab_Activity_map_BalloonItemizedOverlay<OverlayItem> {           

    private List<OverlayItem> mOverlays;

private Context c;


public Tab_Activity_map_HelloItemizedOverlay(Drawable defaultMarker,  MapView mapView) {
    super(boundCenter(defaultMarker), mapView);
    c = mapView.getContext();
    mOverlays = new ArrayList();
    // TODO Auto-generated constructor stub
}

@Override
protected  OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

@Override
public int size() {
    return mOverlays.size();
}

public  void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

public  void removeOverlay(OverlayItem overlay) {
    mOverlays.remove(overlay);
    populate();
}

public  void clear() {
    mOverlays.clear();
    populate();
}

protected boolean onBalloonTap(int index, OverlayItem item) {
    Toast.makeText(c, "onBalloonTap for overlay index " + index, Toast.LENGTH_LONG).show();
    return true;
}
}

这是绘制位置的方法:

public void createAndShowMyItemizedOverlay(Location newLocation, String User, String time) {
    List overlays = mapView2.getOverlays();

    // first remove old overlay
    if (overlays.size() > 0) {
        for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
            iterator.next();
            iterator.remove();
        }
    }
    // transform the location to a geopoint
    GeoPoint geopoint = new GeoPoint((int) (newLocation.getLatitude() * 1E6), (int) (newLocation.getLongitude() * 1E6));

    // initialize icon
    Drawable icon = getResources().getDrawable(R.drawable.marker_friends);
    icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());

    // create my overlay and show it
    //Tab_Activity_map_MyItemizedOverlay overlay= new Tab_Activity_map_MyItemizedOverlay(icon);
    Tab_Activity_map_HelloItemizedOverlay overlay = new Tab_Activity_map_HelloItemizedOverlay(icon, mapView2);
    OverlayItem item = new OverlayItem(geopoint, User + "\n" + time, null);
    overlay.addOverlay(item);
    //overlay.addItem(item);
    mapView2.getOverlays().add(overlay);

    // redraw map
    mapView2.postInvalidate();
}

使用两个不同的mapViews(mapView和mapView2),否则draw方法会删除myLocationOverlay点mapView2.postInvalidate();

正如我所说,我获得了ConcurrentModificationException。我找到了一些解决方案,但我不知道如何申请......这对我来说非常重要,因为这是我职业生涯的结束。非常感谢你!

1 个答案:

答案 0 :(得分:0)

变化:

// first remove old overlay
if (overlays.size() > 0) {
    for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
        iterator.next();
        iterator.remove();
    }
}

要:

overlays.clear();