java - Getting ConcurrentModificationException from onLocationMethod -


this question has answer here:

i trying update arraylist of locations per below logic in service, running in background thread through runnable.i have edited code show relevant code. getting concurrentmodificationexception.

public static arraylist<location> locationpoints;   @override public void onlocationchanged(final location location) {     //log.i(tag, "onlocationchanged: " + location);     log.i(tag, "onlocationchanged: " + locationpoints.size());      arraylist<location> alnew= locationpoints;      if(!locationpoints.isempty()){         for(location l:alnew){             if(location.distanceto(l)<=200.0f){                 locationpoints.add(l);             }else{                 locationpoints.add(location);             }         }     }else{         locationpoints.add(location);     }      sendlocationstoactivity(locationpoints); } 

i want location objects going 1 after other, see location objects increasing exponentially.

result log in onlocationchanged below. exception emanating arraylist methods unable find solution after using remedies given here.

onlocationchanged: 0
onlocationchanged: 1
onlocationchanged: 2
onlocationchanged: 4
onlocationchanged: 8
onlocationchanged: 16
onlocationchanged: 32

but if delete arraylist logic onlocationchanged , add location objects arraylist, results way want

onlocationchanged: 0
onlocationchanged: 1
onlocationchanged: 2
onlocationchanged: 3
onlocationchanged: 4
onlocationchanged: 5

stacktrace:

fatal exception: main process: com.amreen.test, pid: 27053 theme: themes:{default=overlay:com.resurrectionremix.pitchblack, fontpkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.headers=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack} java.util.concurrentmodificationexception     @ java.util.arraylist$arraylistiterator.next(arraylist.java:573)     @ com.amreen.test.mylocationservice.onlocationchanged(mylocationservice.java:146)     @ com.google.android.gms.location.internal.zzk$zzb.handlemessage(unknown source)     @ android.os.handler.dispatchmessage(handler.java:102)     @ android.os.looper.loop(looper.java:148)     @ android.app.activitythread.main(activitythread.java:5458)     @ java.lang.reflect.method.invoke(native method)     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 

concurrentmodificationexception occurs when modify list (adding or removing elements) while traversing list iterator. for-each loop nothing syntactic sugar java.util.iterator. here logic like:

for() {      if(somecondition) {          locationpoints.add(sth);    } } 

rather can try sth like:

for() {          if(somecondition) {              someotherarraylist.add(sth);        }     } 

and once out of loop , then:

locationpoints.addall(someotherarraylist); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -