Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - setVisibility results in java.util.ConcurrentModificationException
    primarykey
    data
    text
    <p>I am hiding a view via <code>setVisibility(View.INVISIBLE)</code>. Later on when I try to show the view again in a different method via <code>setVisibility(View.VISIBLE)</code> I get the following exception</p> <pre><code>03-28 01:32:05.450: E/AndroidRuntime(20895): FATAL EXCEPTION: main 03-28 01:32:05.450: E/AndroidRuntime(20895): java.util.ConcurrentModificationException 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796) 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.util.HashMap$KeyIterator.next(HashMap.java:823) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:946) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewRoot.handleDragEvent(ViewRoot.java:3027) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewRoot.handleMessage(ViewRoot.java:2185) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.os.Handler.dispatchMessage(Handler.java:99) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.os.Looper.loop(Looper.java:132) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.app.ActivityThread.main(ActivityThread.java:4028) 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.lang.reflect.Method.invokeNative(Native Method) 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.lang.reflect.Method.invoke(Method.java:491) 03-28 01:32:05.450: E/AndroidRuntime(20895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 03-28 01:32:05.450: E/AndroidRuntime(20895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 03-28 01:32:05.450: E/AndroidRuntime(20895): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>When I comment out the line that changes the visibility back to visible, I don't get the exception.</p> <p>I first thought that the exception would be caused by some other code iterating through a hashmap, however, I don't do any modifications while iterating through the hashmaps I use, neither do I have multithreading, which seem to be the most common reason for this exception. Also I don't get the exception when I don't change back the visibility.</p> <p><strong>EDIT</strong>:<br> The exception occurs in a custom fragment. Below is the code where I iterate over the hashmap (<code>mWidgetConfig</code>) that contains information about the configuration of custom widgets that I am trying to restore. The hashmap is a public variable in the fragment.</p> <p>In an <code>OnDragListener</code> which is created by the fragment, I update the hashmap according to a certain drag operation, like this:</p> <pre><code>// Update the widget configuration of the fragment that created this listener mFragment.mWidgetConfig.put(startCircleTag, "0"); </code></pre> <p>I also iterate over the hashmap to check a certain condition but I don't do any modification during the iteration:</p> <pre><code>Iterator&lt;String&gt; keySetItr = mFragment.mWidgetConfig.keySet().iterator(); while(keySetItr.hasNext()) { String tag = keySetItr.next(); if(mFragment.mWidgetConfig.get(tag).equals((String) destSocket.getTag())) { // do something, though no modification of the hashmap break; } } </code></pre> <p>In addition I do one iteration in the fragment itself while trying to restore the widget configuration. Below is the code I use to configure the widget according to the hashmap:</p> <pre><code> public void configureWidgets() { resetWidgets(); Iterator&lt;String&gt; keySetItr = mWidgetConfig.keySet().iterator(); while(keySetItr.hasNext()) { String tag = keySetItr.next(); Integer value = Integer.parseInt(mWidgetConfig.get(tag)); ImageView destSocket = null; switch(value) { case 0: // The circle will not be connected to any socket continue; case 1: destSocket = mSocket1; break; case 2: destSocket = mSocket2; break; case 3: destSocket = mSocket3; break; } ImageView startCircle = (ImageView) mLayout.findViewWithTag(tag); ImageView startPlug = (ImageView) mLayout.findViewWithTag(tag + "_plug"); // Replace the drawable of destSocket destSocket.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket_plugged)); // Hide plug view startPlug.setVisibility(View.INVISIBLE); // Draw a line between the start circle view and the destination socket view mConnectionLinesView.addLine(startCircle, destSocket); } } public void resetWidgets() { // Remove all lines mConnectionLinesView.removeLines(); // Show all eventually previously hidden plugs //mPlug1.setVisibility(View.VISIBLE); //mPlug2.setVisibility(View.VISIBLE); //mPlug3.setVisibility(View.VISIBLE); // Set to backround drawable of the socket to the initial one mSocket1.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket).mutate()); mSocket2.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket).mutate()); mSocket3.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket).mutate()); } </code></pre> <p>As soon as the lines that set the visibility of the "plugs" above are used in the code, I get the exception.</p> <p><strong>SOLUTION</strong><br> The reason the exception got thrown is that I called the configuration methods in the <code>DragEvent.ACTION_DRAG_ENDED</code> case statement of the <code>OnDragListener</code>. When I put the same code into the <code>DragEvent.ACTION_DROP</code> case statement the exception doesn't get thrown. No clue why. Thanks for your help guys</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload