我在ArrayList上做了一个非常简单的“过滤器”,偶尔会抛出一个ConcurrentModifificationException,但是AFAICT没有发生任何修改......代码非常简单......
public ArrayList<JSONObject> getContributionsByInstallation(int installationID, ArrayList<JSONObject> list){
ArrayList<JSONObject> filtered = new ArrayList<JSONObject>();
for(JSONObject object : list) {
try {
int fk = object.getInt("exhibit_fk");
if(fk == installationID){
filtered.add(object);
}
} catch(JSONException e){
}
}
return filtered;
}
我错过了什么?
/编辑:添加堆栈跟踪
07-12 12:39:24.135: E/AndroidRuntime(2350): FATAL EXCEPTION: main
07-12 12:39:24.135: E/AndroidRuntime(2350): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whatever.light/com.whatever.light.InstallationDetailActivity}: java.util.ConcurrentModificationException
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.os.Looper.loop(Looper.java:130)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-12 12:39:24.135: E/AndroidRuntime(2350): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 12:39:24.135: E/AndroidRuntime(2350): at java.lang.reflect.Method.invoke(Method.java:507)
07-12 12:39:24.135: E/AndroidRuntime(2350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-12 12:39:24.135: E/AndroidRuntime(2350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-12 12:39:24.135: E/AndroidRuntime(2350): at dalvik.system.NativeStart.main(Native Method)
07-12 12:39:24.135: E/AndroidRuntime(2350): Caused by: java.util.ConcurrentModificationException
07-12 12:39:24.135: E/AndroidRuntime(2350): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576)
07-12 12:39:24.135: E/AndroidRuntime(2350): at com.whatever.light.data.ContributionsData.getContrubtionsByInstallation(ContributionsData.java:81)
07-12 12:39:24.135: E/AndroidRuntime(2350): at com.whatever.light.data.ContributionsData.getRandomCommentContribution(ContributionsData.java:62)
07-12 12:39:24.135: E/AndroidRuntime(2350): at com.whatever.light.installations.ContributeCommentModuleFragment.showComment(ContributeCommentModuleFragment.java:102)
07-12 12:39:24.135: E/AndroidRuntime(2350): at com.whatever.light.installations.ContributeCommentModuleFragment.onCreateView(ContributeCommentModuleFragment.java:86)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:844)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1371)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:499)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.Activity.performStart(Activity.java:3817)
07-12 12:39:24.135: E/AndroidRuntime(2350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
07-12 12:39:24.135: E/AndroidRuntime(2350): ... 11 more
答案 0 :(得分:1)
几乎可以肯定,另一个线程正在修改list
引用的对象 - 这种方法不是。寻找可能发生的所有可能方式。
答案 1 :(得分:0)
你也可以使用CopyOnWriteArrayList来避免concurrentModificationException ..你可以参考这个链接,它说明了arrayList和CopyOnWriteArrayList ..
http://techvivek.wordpress.com/2009/08/29/difference-between-arraylist-and-copyonwritearraylist/