我尝试调试几个代码并给出并发修改异常
码
final GestureLibrary store1 = GestureLibraries.fromFile(new File(Environment.getExternalStorageDirectory(), "Gesture_launcher"));
if (store1.load())
{
for (String name : store1.getGestureEntries())
{
for (Gesture gesture : store1.getGestures(name))
{
if(name.contains(gesture_name))
{
store1.removeGesture(name, gesture);
store1.addGesture(name, mGesture);
store1.save();
Toast.makeText(getApplicationContext(), getResources().getString(R.string.saved), Toast.LENGTH_LONG).show();
}
}
}
}
执行上述语句会产生concurrentModificationException
logcat的
03-28 02:47:45.422: E/AndroidRuntime(449): FATAL EXCEPTION: main
03-28 02:47:45.422: E/AndroidRuntime(449): java.util.ConcurrentModificationException
03-28 02:47:45.422: E/AndroidRuntime(449): at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796)
03-28 02:47:45.422: E/AndroidRuntime(449): at java.util.HashMap$KeyIterator.next(HashMap.java:823)
03-28 02:47:45.422: E/AndroidRuntime(449): at com.likith.gesturebuilder.change_gestures.setGestures(change_gestures.java:69)
03-28 02:47:45.422: E/AndroidRuntime(449): at com.likith.gesturebuilder.change_gestures$1.onClick(change_gestures.java:110)
03-28 02:47:45.422: E/AndroidRuntime(449): at android.view.View.performClick(View.java:2485)
03-28 02:47:45.422: E/AndroidRuntime(449): at android.view.View$PerformClick.run(View.java:9080)
03-28 02:47:45.422: E/AndroidRuntime(449): at android.os.Handler.handleCallback(Handler.java:587)
03-28 02:47:45.422: E/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 02:47:45.422: E/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:123)
03-28 02:47:45.422: E/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 02:47:45.422: E/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 02:47:45.422: E/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:507)
03-28 02:47:45.422: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-28 02:47:45.422: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-28 02:47:45.422: E/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method)
请告诉我们如何避免此异常的任何方法 谢谢
编辑1: 现在我修改了代码
List<Gesture> list = store1.getGestures(gesture_name);
Set<String> List_name = store1.getGestureEntries();
for (String name : List_name)
{
for (Gesture gesture : list)
{
if(name.contains(gesture_name))
{
store1.removeGesture(name, gesture);
store1.addGesture(name, mGesture);
store1.save();
Toast.makeText(getApplicationContext(), getResources().getString(R.string.saved), Toast.LENGTH_LONG).show();
}
}
}
它再次给了我同样的错误。
答案 0 :(得分:2)
您在调用store1.removeGesture()和store1.addGesture()的同时循环遍历store1.getGestures()。
尝试复制手势数组并循环复制。
Array<Gesture> newArray = new Array<>();
newArray.addAll(store1.getGestures(name));
for (Gesture gesture : newArray)
{
if(name.contains(gesture_name))
{
store1.removeGesture(name, gesture);
store1.addGesture(name, mGesture);
store1.save();
...
答案 1 :(得分:0)
避免并发修改异常的一种简单方法是使用常规for循环。
使用ehanced for循环时,使用迭代器。使用迭代器时,不允许修改集合。
常规for循环看起来像这样
List<String> list;
for(int i = 0; i < list.size(); i++) {
String string = list.get(i);
//use string
}
与使用增强的for循环相反,它看起来像这样
List<String> list;
for(String string : list) {
//use string
}