本文介绍了并发修改异常:添加到 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题发生在

Element element = it.next();

包含该行的这段代码位于 OnTouchEvent

And this code which contains that line, is inside of an OnTouchEvent

for (Iterator<Element> it = mElements.iterator(); it.hasNext();){
    Element element = it.next();

    if(touchX > element.mX  && touchX < element.mX + element.mBitmap.getWidth() && touchY > element.mY
            && touchY < element.mY + element.mBitmap.getHeight()) {

        //irrelevant stuff..

        if(element.cFlag){
            mElements.add(new Element("crack",getResources(), (int)touchX,(int)touchY));
            element.cFlag = false;

        }
    }
}

所有这些都在 synchronized(mElements) 中,其中 mElements 是一个 ArrayList

All of this is inside synchronized(mElements), where mElements is an ArrayList<Element>

当我触摸一个Element时,它可能会激活cFlag,这将创建另一个具有不同属性的Element,它会从屏幕上掉下来并在不到一秒的时间内摧毁自己.这是我创建粒子效果的方式.我们可以称之为粒子"crack,就像构造函数中的String参数一样.

When I touch an Element, it may activate cFlag, which will create another Element with different properties, which will fall off the screen and destroy itself in less than a second. It's my way of creating particle effects. We can call this "particle" crack, like the String parameter in the constructor.

这一切正常,直到我添加另一个主要的 Element.现在我在屏幕上同时有两个 Element,如果我触摸最新的 Element,它工作正常,并启动粒子.

This all works fine until I add another main Element. Now I have two Elements on the screen at the same time, and if I touch the newest Element, it works fine, and launches the particles.

但是,如果我在旧的 Element 上触摸并激活 cFlag,则会出现异常.

However, if I touch and activate cFlag on the older Element, then it gives me the exception.

 07-28 15:36:59.815: ERROR/AndroidRuntime(4026): FATAL EXCEPTION: main
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): java.util.ConcurrentModificationException
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.Juggle2.Panel.onTouchEvent(Panel.java:823)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.View.dispatchTouchEvent(View.java:3766)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1767)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1119)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1751)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.os.Looper.loop(Looper.java:123)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.lang.reflect.Method.invoke(Method.java:521)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at dalvik.system.NativeStart.main(Native Method)

我怎样才能做到这一点?

How can I make this work?

推荐答案

ConcurrentModificationException 当您在使用 Iterator 遍历列表时修改列表(通过添加或删除元素).

ConcurrentModificationException occurs when you modify the list (by adding or removing elements) while traversing a list with Iterator.

试试

List<Element> thingsToBeAdd = new ArrayList<Element>();
for(Iterator<Element> it = mElements.iterator(); it.hasNext();) {
    Element element = it.next();
    if(...) {
        //irrelevant stuff..
        if(element.cFlag){
            // mElements.add(new Element("crack",getResources(), (int)touchX,(int)touchY));
            thingsToBeAdd.add(new Element("crack",getResources(), (int)touchX,(int)touchY));
            element.cFlag = false;
        }
    }
}
mElements.addAll(thingsToBeAdd );

此外,您应该按照 Jon 的建议考虑对每个循环进行增强.

Also you should consider enhanced for each loop as Jon suggested.

这篇关于并发修改异常:添加到 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:04