我的应用程序在 Android 1.6 上运行良好,但是当我在 Android 2.2 上运行它时,我在某些地方得到了 concurrentModificationException。经过几天的思考,我想出了一个理论:我猜多线程(或类似的东西)从 1.6 开始就已经引入了。会是这样吗?如果是这样,是否有任何方法可以强制应用程序在没有多线程的情况下运行?我尝试将目标设置为 1.6,但没有成功。在此先感谢您抽出宝贵时间。
例外:
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): FATAL EXCEPTION: main
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { cmp=ntnu.client/com.google.android.maps.MapView (has extras) }} to activity {ntnu.client/ntnu.client.MapClient}: java.util.ConcurrentModificationException
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.ActivityThread.deliverResults(ActivityThread.java:3808)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3850)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.ActivityThread.access$2800(ActivityThread.java:136)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2209)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.os.Looper.loop(Looper.java:143)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.ActivityThread.main(ActivityThread.java:5068)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at java.lang.reflect.Method.invoke(Method.java:521)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at dalvik.system.NativeStart.main(Native Method)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): Caused by: java.util.ConcurrentModificationException
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at ntnu.client.MapClient.handleResult(MapClient.java:599)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at ntnu.client.MapClient.onActivityResult(MapClient.java:881)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.Activity.dispatchActivityResult(Activity.java:3988)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): at android.app.ActivityThread.deliverResults(ActivityThread.java:3804)
04-05 11:47:12.812: ERROR/AndroidRuntime(5328): ... 11 more
下面提供了带有 handleresult-code 的代码。
public synchronized void handleResult(boolean notify)
{
if(!citynodes.equals(null) && citynodes.size()>0 )
{
noteBaloon.setVisibility(0x00000008);
Drawable drawable = this.getResources().getDrawable(R.drawable.up);
Context myContext = this;
itemizedoverlay = new CitynodeItemizedOverlay(drawable,myContext);
itemizedoverlay.setThumbsUp(BitmapFactory.decodeResource(
getResources(), R.drawable.vote_yes3));
itemizedoverlay.setThubmsDown(BitmapFactory.decodeResource(
getResources(), R.drawable.vote_no3));
itemizedoverlay.addObserver(this);
//itemizedoverlay.setDoAnimtation(true);
RecommendationNotificationOverlay overlay = new RecommendationNotificationOverlay();
for (Recommendation n : citynodes )
{
CitynodeOverlayItem cn= n.getNode().getOverlayItem();
Drawable marker = this.iconmanager.changeBackground(this.iconmanager.findIcon(n.getNode()),Integer.parseInt(n.getSystemRating()),n.isPersonalized());
//marker.setAlpha(100);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
ShapeDrawable l;
cn.setMarker(marker);
cn.setNode(n);
itemizedoverlay.addOverlay(cn);
LayoutInflater inflater = getLayoutInflater();
}
for(Overlay i : getMapView().getOverlays() )
{
if((i instanceof ItemizedOverlay)) //|| (i instanceof RecommendationNotificationOverlay) )
this.mapView.getOverlays().remove(i);
}
for(Overlay i : getMapView().getOverlays() )
{
if((i instanceof RecommendationNotificationOverlay) )
this.mapView.getOverlays().remove(i);
}
List <Recommendation> proactive = new ArrayList<Recommendation>();
for(Recommendation potpro : this.citynodes)
{
if(potpro.isProactive())
{
proactive.add(potpro);
}
}
overlay.setNotifications(proactive);
mapOverlays.add(overlay);
mapOverlays.add(itemizedoverlay);
mapView.invalidate();
}
}
此异常引用的代码是第一个 for 循环:
for(Overlay i : getMapView().getOverlays() )
该代码还在这一行引发了相同的异常(处理另一个操作):
if(!citynodes.equals(null) && citynodes.size()>0 )