Java.util.ConcurrentModificationException On MapView

guys

I came across a very strange question from many days. I often try to update the overlay. Therefore, sometime I get a "java.util.ConcurrentModificationException" when I touch the map or sometime when the map tries to update the overlay. But I do not find the ideal line that comes with this error.

02-17 14:56:01.621: W/dalvikvm(3653): threadid=1: thread exiting with uncaught exception (group=0x40015560) 02-17 14:56:01.631: E/AndroidRuntime(3653): FATAL EXCEPTION: main 02-17 14:56:01.631: E/AndroidRuntime(3653): java.util.ConcurrentModificationException 02-17 14:56:01.631: E/AndroidRuntime(3653): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.google.android.maps.MapView.onDraw(MapView.java:530) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6880) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6883) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6883) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6883) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewRoot.draw(ViewRoot.java:1522) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.os.Handler.dispatchMessage(Handler.java:99) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.os.Looper.loop(Looper.java:130) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.app.ActivityThread.main(ActivityThread.java:3683) 02-17 14:56:01.631: E/AndroidRuntime(3653): at java.lang.reflect.Method.invokeNative(Native Method) 02-17 14:56:01.631: E/AndroidRuntime(3653): at java.lang.reflect.Method.invoke(Method.java:507) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 02-17 14:56:01.631: E/AndroidRuntime(3653): at dalvik.system.NativeStart.main(Native Method) 

I use balloon overlay

I think it throws an error on "mapview.getOverlay()" because it calls 4-5 times in 1 second.

Thanks,

Caproid

here is the code stream. My theme code is ....

 Thread connection = new Thread(){ public void run() { try { while (my condition) { try { //This method should be call every seconds updateMethod(); } catch (Exception e) { e.printStackTrace(); break; } } } catch (Exception e) { } } }; 

Here is my updateMethod (); in this code hashmapOverlay is a HashMap as shown below

  public static Map<String,MyItemizedOverlay> hashmapOverlay; public void updateMethod() { if(hashmapOverlay.containsKey(id)) { mapview.getOverlays().remove(hashmapOnlineFriendsOverlay.get(id)); } MyItemizedOverlay mMyItemizedOverlay = new MyItemizedOverlay(drawable, mapview); OverlayItem overlayItem = new OverlayItem(gp,title ,snippet); mMyItemizedOverlay.addOverlay(overlayItem); hashmapOverlay.put(id, mMyItemizedOverlay); addOverlayMethod(mActivity, mapView, mMyItemizedOverlay); } 

Here is my addOverlayMethod

 addOverlayMethod(Activity mActivity, final MapView mapView, final Object mObject) { mActivity.runOnUiThread(new Runnable(){ @Override public void run() { try { MyItemizedOverlay overlay = (MyItemizedOverlay) mObject; mapView.getOverlays().add(overlay); } catch (Exception e) {} } }); } 
+4
source share
5 answers

ya, you can work step by step.

  • Create the Pendingnotification class (modification detail).
  • Make a list of Pendingnotification.
  • when any notification is received by dnt, change it directly, collect all the changes and put the stack (Pendingnotification class), i.e. add it to the Pendingnotification list.
  • Now, one after the other, they change the object and delete the same object when doing its work.

the same goes until the Pendingnotification list is empty.

Also note that any modification must be performed in UI Thread not in NonUI Thread

+2
source

To avoid a ConcurrentModificationException simply copy your collection in any of the following cases:

  • before iterating over the collection
  • before passing it to the API

At first glance this may seem redundant, but in the future it will save you a lot of headache. Copying is simple, just call new ArrayList<YourClass>(existingList) .

+2
source

I think you use Thread and create it Concurrency So use Syncronized(MainActivity.this) in your launch method ... And use a vector instead of a list because the vector is synchronized ... Its working for me ...

+1
source

Well, it looks like in your updateMethod() you are still trying Update use your UI from the Non-UI stream on

 mapview.getOverlays().remove(hashmapOnlineFriendsOverlay.get(id)); 

So, try to execute this piece of code inside RunOnUiThread and try. Hope this works.

 UPDATE: 

Also, why are you trying to add and remove all Overlay all, you don’t have to do this, just try updating your Overlay and invalidate . Have a look at this example.

+1
source

I'm not sure about managing the overlay, but as I see it, you modify your HashMap using HashMap.put in a thread other than the UI (in UpdateMethod) when it is undoubtedly repeated or used elsewhere in the application - possibly when You are still requesting new Overlays when moving your card.

If you read docs , he says that this will throw a ConcurrentModificationException. Instead, try using ConcurrentHashMap or rebuild your code to update hashmap in the main thread where it is used.

+1
source

Source: https://habr.com/ru/post/1397005/


All Articles