相关疑难解决方法(0)

如何在迭代时从"ArrayList"中删除元素时避免"ConcurrentModificationException"?

我试图删除一些元素ArrayList迭代它像这样:

for (String str : myArrayList) {
    if (someCondition) {
        myArrayList.remove(str);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,我ConcurrentModificationException试图在迭代时同时从列表中删除项目时得到一个myArrayList.有没有一些简单的解决方案来解决这个问题?

java iterator list arraylist

333
推荐指数
9
解决办法
38万
查看次数

Android动画中的java.util.ConcurrentModificationException

我在Android中同步代码的概念中遗漏了一些东西.

脚本

屏幕上总是绘制3个项目.每个图像都存储在ArrayList(lstGraphics)中.为此,我使用SurfaceView.一旦用户点击图像,图像就会被移除市场并且将添加新的市场.

代码示例:

AnimationHideThread

...
    @Override
        public void run() {
            Canvas c;
            while (run) {
                c = null;
                try {
                    c = panel.getHolder().lockCanvas(null);
                      synchronized (panel.getHolder()) {

                        panel.updatePhysics();
                        panel.manageAnimations();
                        panel.onDraw(c);

                    }
                } finally {
                    if (c != null) {
                        panel.getHolder().unlockCanvasAndPost(c);
                    }
                }
            }
        }    
...
Run Code Online (Sandbox Code Playgroud)

所以你可以先看看updatePhysics().这意味着我计算每个图像移动到的方向.在这里,我还将删除列表中的点击图像.之后,我检查是否需要在manageAnimations()的列表中添加一个新项目,然后最后一步绘制整个事物.

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
....
 public void manageAnimations()
    {
          synchronized (this.getHolder()) {
            ...
        while (lstGraphics.size()<3) {
                lstGraphics.add(createRandomGraphic());
                }
        }
          }
    }

 @Override
    public boolean onTouchEvent(MotionEvent event) {
         synchronized (getHolder()) { …
Run Code Online (Sandbox Code Playgroud)

animation android concurrentmodification

23
推荐指数
2
解决办法
3万
查看次数

View.setVisibility中的java.util.ConcurrentModificationException

我正在为观点实施"拖放".当拖动开始时,我将视图的可见性设置为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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和:

if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
    View droppedView = (View) event.getLocalState();
    droppedView.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

当调用"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): …
Run Code Online (Sandbox Code Playgroud)

android drag-and-drop view

19
推荐指数
1
解决办法
3858
查看次数

Java:迭代列表时出现ConcurrentModificationException

当我执行以下代码时,我得到ConcurrentModificationException

 Collection<String> myCollection = Collections.synchronizedList(new ArrayList<String>(10));
    myCollection.add("123");
    myCollection.add("456");
    myCollection.add("789");
    for (Iterator it = myCollection.iterator(); it.hasNext();) {
        String myObject = (String)it.next();
        System.out.println(myObject);
        myCollection.remove(myObject); 
        //it.remove();
    }
Run Code Online (Sandbox Code Playgroud)

为什么我得到异常,即使我使用Collections.synchronizedList?

当我将myCollection更改为

  ConcurrentLinkedQueue<String> myCollection = new ConcurrentLinkedQueue<String>();
Run Code Online (Sandbox Code Playgroud)

我没有得到那个例外.

java.util.concurrent中的ConcurrentLinkedQueue与Collections.synchronizedList有何不同?

java collections

8
推荐指数
2
解决办法
1万
查看次数