java - Concurrent Modification Exception : adding to an ArrayList -
the problem occurs @
element element = it.next();
and code contains line, inside of ontouchevent
for (iterator<element> = 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; } } }
all of inside synchronized(melements)
, melements
arraylist<element>
when touch element
, may activate cflag
, create element
different properties, fall off screen , destroy in less second. it's way of creating particle effects. can call "particle" crack
, string parameter in constructor.
this works fine until add main element
. have 2 elements
on screen @ same time, , if touch newest element
, works fine, , launches particles.
however, if touch , activate cflag
on older element
, gives me 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): @ java.util.arraylist$arraylistiterator.next(arraylist.java:573) 07-28 15:36:59.815: error/androidruntime(4026): @ com.juggle2.panel.ontouchevent(panel.java:823) 07-28 15:36:59.815: error/androidruntime(4026): @ android.view.view.dispatchtouchevent(view.java:3766) 07-28 15:36:59.815: error/androidruntime(4026): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:863) 07-28 15:36:59.815: error/androidruntime(4026): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:863) 07-28 15:36:59.815: error/androidruntime(4026): @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:1767) 07-28 15:36:59.815: error/androidruntime(4026): @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1119) 07-28 15:36:59.815: error/androidruntime(4026): @ android.app.activity.dispatchtouchevent(activity.java:2086) 07-28 15:36:59.815: error/androidruntime(4026): @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:1751) 07-28 15:36:59.815: error/androidruntime(4026): @ android.view.viewroot.handlemessage(viewroot.java:1785) 07-28 15:36:59.815: error/androidruntime(4026): @ android.os.handler.dispatchmessage(handler.java:99) 07-28 15:36:59.815: error/androidruntime(4026): @ android.os.looper.loop(looper.java:123) 07-28 15:36:59.815: error/androidruntime(4026): @ android.app.activitythread.main(activitythread.java:4627) 07-28 15:36:59.815: error/androidruntime(4026): @ java.lang.reflect.method.invokenative(native method) 07-28 15:36:59.815: error/androidruntime(4026): @ java.lang.reflect.method.invoke(method.java:521) 07-28 15:36:59.815: error/androidruntime(4026): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:893) 07-28 15:36:59.815: error/androidruntime(4026): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:651) 07-28 15:36:59.815: error/androidruntime(4026): @ dalvik.system.nativestart.main(native method)
how can make work?
concurrentmodificationexception occurs when modify list (by adding or removing elements) while traversing list iterator
.
try
list<element> thingstobeadd = new arraylist<element>(); for(iterator<element> = 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 );
also should consider enhanced each loop jon suggested.
Comments
Post a Comment