将视图设置为可见

时间:2016-12-28 02:28:14

标签: android listview thread-synchronization java-threads

我有两个ListFragments和6个视图都设置为onDragListeners。如果用户将这6个视图中的一个拖到ActionBar上,则会引发ConcurrentModificationException。这是因为我已将这6个视图中的每个视图设置为侦听DragEvent.ACTION_DRAG_ENDED,如果event.getResult()为false,则会尝试使该视图可见:

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

截至当前,isFirstModify永远不会设置为true,上面的代码仍然会抛出ConcurrentModificationException。所以,我想知道为什么上面的代码抛出异常?可能每个监听器的响应都在同一个线程上运行?

我在ConcurrentModificationException得到java.util.HashMap$HashIterator.nextEntry,因此,由于编辑视图而另一个线程正在迭代它,可能会抛出异常。唯一的问题是理论是当只有一个侦听器将视图设置为可见时,不会抛出任何异常。同样,我只有一个实体来修改视图,因为有两个ListFragments附加到一个监听器类,另外六个视图附加到另一个监听器类,只有这两个类监听器调用DragEvent.ACTION_DRAG_ENDED放下观点。

所以,我看到两种可能的解决方案,我还没有能够开始工作。
1。一旦一个监听器修改了视图,阻止其他监听器修改视图或者2。只调用一个监听器来修改视图。

编辑: 完整的错误堆栈消息:

  

致命的例外:主要                                                                                     处理: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)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1189)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1191)                                                                                         在android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5032)                                                                                         在android.view.ViewRootImpl.access $ 800(ViewRootImpl.java:105)                                                                                         在   android.view.ViewRootImpl $ ViewRootHandler.handleMessage(ViewRootImpl.java:3266)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                         在android.os.Looper.loop(Looper.java:136)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5086)                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)                                                                                         在java.lang.reflect.Method.invoke(Method.java:515)                                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                                                                                         在dalvik.system.NativeStart.main(本地方法)

0 个答案:

没有答案