View.setVisibility 中的 java.util.ConcurrentModificationException [英] java.util.ConcurrentModificationException in View.setVisibility

查看:20
本文介绍了View.setVisibility 中的 java.util.ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为视图实现拖放.当拖动开始时,我将视图的可见性设置为 INVISIBLE,然后,如果拖动被中断 - 返回 VISIBLE:

public boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {//跳过了一些代码boolean dragStarted = v.startDrag(data, shadowBuilder, v, 0);如果(拖动开始){v.setVisibility(View.INVISIBLE)}}}

还有:

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

当拖动结束"事件被调用时,我收到异常:

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

为什么以及如何修复它?

解决方案

你可以试试这个

if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {最终视图 dropView = (View) event.getLocalState();dropView.post(new Runnable(){@覆盖公共无效运行(){dropView.setVisibility(View.VISIBLE);}});}

看起来像 Android 本身试图在您结束拖动的同时访问视图状态.

编辑

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

I'm implementing drag'n'drop for views. When drag is started, I set visibility of the view to INVISIBLE, then, if the drag was interrupted - back to 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)
        }
    }
}

And:

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

And when "Drag ended" event is called, I'm getting exception:

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)

Why and how to fix it?

解决方案

You can try this

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);
        }
    });
}

Looks like Android itself trying to access View state at the same time as you end dragging.

EDIT

More precise explanation. By setting setVisibility(), you're including or excluding View from Android internal collection of views, that should respond to drag events. This collection is used during dispatch of drag events, and therefore by trying to setVisibility (in other words, trying to modify listeners of drag events) you're causing ConcurrentModificationException

这篇关于View.setVisibility 中的 java.util.ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆