View.setVisibility中的java.util.ConcurrentModificationException

时间:2012-06-11 23:16:10

标签: android view drag-and-drop

我正在为观点实施“拖放”。当拖动开始时,我将视图的可见性设置为INVISIBLE,然后,如果拖动被中断,则返回VISIBLE

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // Skipped some code
        boolean dragStarted = v.startDrag(data, shadowBuilder, v, 0);

        if (dragStarted) {
            v.setVisibility(View.INVISIBLE)
        }
    }
}

if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
    View droppedView = (View) event.getLocalState();
    droppedView.setVisibility(View.VISIBLE);
}

当调用“Drag ended”事件时,我会遇到异常:

E/AndroidRuntime(7118): FATAL EXCEPTION: main 
E/AndroidRuntime(7118): java.util.ConcurrentModificationException 
E/AndroidRuntime(7118):     at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792)
E/AndroidRuntime(7118):     at java.util.HashMap$KeyIterator.next(HashMap.java:819) 
E/AndroidRuntime(7118):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1046)
E/AndroidRuntime(7118):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1048)
E/AndroidRuntime(7118):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1048)
E/AndroidRuntime(7118):     at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1048)
E/AndroidRuntime(7118):     at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
E/AndroidRuntime(7118):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2620)
E/AndroidRuntime(7118):     at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(7118):     at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime(7118):     at android.app.ActivityThread.main(ActivityThread.java:4575)
E/AndroidRuntime(7118):     at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(7118):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(7118):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(7118):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(7118):     at dalvik.system.NativeStart.main(NativeMethod)

为什么以及如何解决它?

3 个答案:

答案 0 :(得分:40)

你可以试试这个

if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
    final View droppedView = (View) event.getLocalState();
    droppedView.post(new Runnable(){
        @Override
        public void run() {
            droppedView.setVisibility(View.VISIBLE);
        }
    });
}

看起来Android本身在您结束拖动的同时尝试访问View状态。

修改

更准确的解释。通过设置setVisibility(),您可以在Android内部视图集合中包含或排除View,这应该响应拖动事件。在发送拖动事件期间使用此集合,因此尝试setVisibility(换句话说,尝试修改拖动事件的侦听器)导致ConcurrentModificationException

答案 1 :(得分:0)

最好的方法是:

    view.post(new Runnable() {
  public void run() {
    view.setVisibility(View.VISIBLE);
  }
});

如果我们使用:

 if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
    final View droppedView = (View) event.getLocalState();
    droppedView.post(new Runnable(){
        @Override
        public void run() {
            droppedView.setVisibility(View.VISIBLE);
        }
    });
}

您仍将强制关闭Null.pointer。 ...

答案 2 :(得分:-1)

也许this可以提供帮助。这里给定的链接说:而不是DragEvent.ACTION_DRAG_ENDED使用DragEvent.ACTION_DROP