java.util.ConcurrentModificationException in View.setVisibility

前端 未结 3 1120
故里飘歌
故里飘歌 2020-12-05 07:33

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 VI

相关标签:
3条回答
  • 2020-12-05 07:53

    The best way is :

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

    if we use:

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

    you will still get force close Null.pointer. ...

    0 讨论(0)
  • 2020-12-05 07:59

    Maybe this can help. here in the given link says: instead of DragEvent.ACTION_DRAG_ENDED use DragEvent.ACTION_DROP.

    0 讨论(0)
  • 2020-12-05 08:06

    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

    0 讨论(0)
提交回复
热议问题