ConcurrentModificationException thrown when setting a view to visible

65 views Asked by At

I have two ListFragments and 6 views all set with onDragListeners. If a user drags one of these 6 views onto the ActionBar, a ConcurrentModificationException is thrown. This is because I have set each of these 6 views to listen for DragEvent.ACTION_DRAG_ENDED and if event.getResult() is false, it will attempt to make that view visible:

public static boolean isFirstModify = true;

public boolean onDrag(View v, DragEvent event) {
    View view = (View) event.getLocalState();

    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_ENDED:
            if (!event.getResult()) {
                synchronized (view) {
                    if (isFirstModify) {
                        isFirstModify = false;
                        view.setVisibility(View.VISIBLE);
                    }
                }
            }
            break;
    }
}

As of current, isFirstModify is never set back to true and the above code still throws a ConcurrentModificationException. So, I was wondering why the above code throws the exception? Possibly is each listener's response running on the same thread?

I get the ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry, so, possibly the exception is thrown due to editing the view while another thread is iterating over it. The only issue with this is theory is that when only one listener sets the view to visible, no exception is thrown. Likewise, I was having trouble getting only one entity to modify the view because there are 2 ListFragments attached to a listener class and 6 other Views attached to another listener class, and only these two class listeners call DragEvent.ACTION_DRAG_ENDED upon dropping of the view.

So, I see two possible solutions that I haven't been able to get working yet.
1. once one listener modifies the view, block the other listeners from modifying the view or
2. only call one listener to modify the view.

Edit: full error stack message:

FATAL EXCEPTION: main Process: com.example.travisho.dragdropui3, PID: 13599 java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806) at java.util.HashMap$KeyIterator.next(HashMap.java:833) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1189) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191) at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5032) at android.view.ViewRootImpl.access$800(ViewRootImpl.java:105) at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3266) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

0

There are 0 answers