0

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)

Charuක
  • 12,953
  • 5
  • 50
  • 88
Travis Ho
  • 105
  • 7
  • Which line is causing the exception? – shmosel Dec 28 '16 at 02:40
  • Synchronizing on `isFirstModify` is a mistake for 2 reasons: 1. As soon as you update it, you no longer have a common reference (synchronization is on the *object*, not on the *variable*). 2. Autoboxing returns a global reference (`Boolean.TRUE`, `Boolean.FALSE`), which is obviously not something you want to synchronize on. – shmosel Dec 28 '16 at 02:43
  • thanks for the suggestion, I just tried synchronizing view because that is a common object but I still got the exception. I edited the above code to match what I did too. – Travis Ho Dec 28 '16 at 02:49
  • See also [Android - setVisibility results in java.util.ConcurrentModificationException](http://stackoverflow.com/q/9899382/1553851) – shmosel Dec 28 '16 at 02:55
  • And [Drag'n'Drop ConcurentModificationException](http://stackoverflow.com/q/25601001/1553851) – shmosel Dec 28 '16 at 02:56
  • Thanks for the help shmosel, first link was very helpful. – Travis Ho Dec 28 '16 at 03:00

0 Answers0