0
首先我要道歉再問這個問題,但我還沒找到解決方案。Android中的ConcurrentModificationException
我有一個應用程序,我可以通過MyLocationOverlay找到自己,並使用itemizedOverlay繪製我的朋友的位置。但是隨着ConcurrentModificationExcepion
對於我的位置的應用程序崩潰我用MyLocationOverlay這個單碼(previusly初始化):
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);
對於我的朋友的位置我使用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),否則平局方法擦除myLocationOverlay點當它mapView2.postInvalidate();
正如我所說的,我獲得了ConcurrentModificationException。我找到了一些解決方案,但我不知道如何申請......這對我來說很重要,因爲這是我職業生涯結束時的工作。非常感謝!
堆棧跟蹤嗎? – Jodes 2011-05-22 21:33:58
堆棧跟蹤確實,你可能必須使用同步塊,因爲你同時從多個線程編輯列表(至少我認爲這是wat導致此錯誤) – matsjoe 2011-05-22 21:36:09
您正在修改集合,同時迭代。 – 2011-05-22 23:25:56