简体   繁体   English

Android中的ConcurrentModificationException

[英]ConcurrentModificationException in Android

First of all I would apologize for ask again this question, but I don't found a solution for my problem yet. 首先,我很抱歉再次提出这个问题,但是我还没有找到解决问题的方法。

I have a app where I can locate myself with a MyLocationOverlay, and I draw my friends' position with a itemizedOverlay. 我有一个应用程序,可以使用MyLocationOverlay定位自己,并使用itemizedOverlay绘制朋友的位置。 But the app crashes with a ConcurrentModificationExcepion 但是该应用因ConcurrentModificationExcepion而崩溃

For my location I use MyLocationOverlay with this single code (previusly initialized): 对于我的位置,我将MyLocationOverlay与以下单个代码结合使用(预先初始化):

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

For my friends' positión I used a ItemizedOverlay and a method to draw the position. 对于我朋友的位置,我使用了ItemizedOverlay和一种绘制位置的方法。 The ItemizedOverlay this is: 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;
}
}

And this is the method to draw the position: 这是绘制位置的方法:

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();
}

Using two differents mapViews (mapView and mapView2) because otherwise the draw method erase the myLocationOverlay dot when it does mapView2.postInvalidate(); 使用两个不同的mapViews(mapView和mapView2),因为否则draw方法在执行mapView2.postInvalidate();时会擦除myLocationOverlay点mapView2.postInvalidate();

As I said I obtain a ConcurrentModificationException. 如我所说,我获得了ConcurrentModificationException。 I found some solutions but I don't know how to apply... It is very important to me because it is for the end of my career working. 我找到了一些解决方案,但是我不知道该如何应用...这对我来说非常重要,因为这是我职业生涯的终结。 Thank you so much! 非常感谢!

Change: 更改:

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

To: 至:

overlays.clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM