Android 动画中的 java.util.ConcurrentModificationException

发布于 2024-10-19 14:00:24 字数 2877 浏览 10 评论 0原文

我怀念 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);
                    }
                }
            }
        }    
...

正如您首先看到的,我更新了Physics()。这意味着我计算每个图像移动的方向。在这里,我还将从列表中删除单击的图像。之后,我检查是否需要在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()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                 //... check if a image has been clicked and then set its property
                        graphic.setTouched(true);

                 }
            }

            return true;
         }
    }

 public void updatePhysics() {
       synchronized (getHolder()) {

     for (Graphic graphic : lstGraphics) {
           //.... Do some checks
     if (graphic.isTouched())
      {
        lstGraphics.remove(graphic);
      }
     }
  }
 }

 @Override
    public void onDraw(Canvas canvas) {
         /// draw the backgrounds and each element from lstGraphics
}

public class Graphic {

        private Bitmap bitmap;
            private boolean touched;
            private Coordinates initialCoordinates; 
....
}

我得到的错误是:

> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): Uncaught handler: thread Thread-12 exiting due to uncaught exception 
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): java.util.ConcurrentModificationException
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:66)
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.Panel.updatePhysics(Panel.java:290)
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.AnimationHideThread.run(AnimationHideThread.java:41)

非常感谢任何帮助。谢谢。

There is something I miss with the notion of Synchronizing code in Android.

Scenario

There are always 3 items drawn on the screen. Each image is stored in a ArrayList (lstGraphics). For this purpose I use a SurfaceView. Once the user taps on a image, the image get's market to be removed and a new one will be added.

Code samples:

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

So as you can seem first I updatePhysics(). This means I calculate direction where each image will move to. In here I will also remove clicked images from my list. After that I check if I need to add a new Item in my list in manageAnimations() and then the final step draw the whole thing.

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()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                 //... check if a image has been clicked and then set its property
                        graphic.setTouched(true);

                 }
            }

            return true;
         }
    }

 public void updatePhysics() {
       synchronized (getHolder()) {

     for (Graphic graphic : lstGraphics) {
           //.... Do some checks
     if (graphic.isTouched())
      {
        lstGraphics.remove(graphic);
      }
     }
  }
 }

 @Override
    public void onDraw(Canvas canvas) {
         /// draw the backgrounds and each element from lstGraphics
}

public class Graphic {

        private Bitmap bitmap;
            private boolean touched;
            private Coordinates initialCoordinates; 
....
}

The error I get is:

> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): Uncaught handler: thread Thread-12 exiting due to uncaught exception 
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): java.util.ConcurrentModificationException
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:66)
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.Panel.updatePhysics(Panel.java:290)
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.AnimationHideThread.run(AnimationHideThread.java:41)

Any help is greatly appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傾旎 2024-10-26 14:00:24

您的问题出在您的物理方法中,您在其中添加图形并列出

public void updatePhysics() {
    synchronized (getHolder()) {
        for (Graphic graphic : lstGraphics) {
        //.... Do some checks
        if (graphic.isTouched()) {
            lstGraphics.remove(graphic); //your problem
        }
    }
}

for(Graphicgraphic : lstGraphics)lst.Graphics.remove(graphic); 的组合导致 ConcurrentModificationException,因为您正在运行列表并同时尝试修改它。

到目前为止,我知道两种解决方案:

  1. 如果有可用的迭代器,请使用迭代器(到目前为止从未针对 Android 进行过编码)。

    while (iter.hasNext) {
        if (物理条件) iter.remove();
    }
    
  2. 使用第二个列表来存储要删除的元素,然后再删除它们

    列表;删除=新....
    for(图形:lstGraphics){
        if (物理条件) {
            toRemove.add(图形);
        }
    }
    lstGraphics.removeAll(toRemove);
    

Your problem is in your physics method, where you add the graphic and the list

public void updatePhysics() {
    synchronized (getHolder()) {
        for (Graphic graphic : lstGraphics) {
        //.... Do some checks
        if (graphic.isTouched()) {
            lstGraphics.remove(graphic); //your problem
        }
    }
}

the combination of for(Graphic graphic : lstGraphics) and lst.Graphics.remove(graphic); causes the ConcurrentModificationException because you are running over your list and concurrently try to modify it.

So far I know two solutions:

  1. Use an Iterator instead if one is available (never coded for Android so far).

    while (iter.hasNext) {
        if (physicsCondition) iter.remove();
    }
    
  2. use a second list to store the elements to remove and remove them afterwards

    List<GraphicsItem> toRemove = new ....
    for (Graphic graphic : lstGraphics) {
        if (physicsCondition) {
            toRemove.add(graphic);
        }
    }
    lstGraphics.removeAll(toRemove);
    
迷离° 2024-10-26 14:00:24

正如 @idefix 所说,您可以在单线程上下文中轻松获得 ConcurrentModificationException,如下所示:

public static void main(String[] args) {
    List<String> list = new ArrayList<String>(Arrays.asList("AAA", "BBB"));
    for (String s : list) {
        if ("BBB".equals(s)) {
            list.remove(s);
        }
    }
}

As @idefix said, you can easily get ConcurrentModificationException in single-threaded context like this:

public static void main(String[] args) {
    List<String> list = new ArrayList<String>(Arrays.asList("AAA", "BBB"));
    for (String s : list) {
        if ("BBB".equals(s)) {
            list.remove(s);
        }
    }
}
我纯我任性 2024-10-26 14:00:24

您可以使用 CopyOnWriteArrayList 如下所示:

    List<String> myList = new CopyOnWriteArrayList<String>();

    myList.add("1");
    myList.add("2");
    myList.add("3");
    myList.add("4");
    myList.add("5");

    Iterator<String> it = myList.iterator();
    while(it.hasNext()){
        String value = it.next();
        System.out.println("List Value:"+value);
        if(value.equals("3")){
            myList.remove("4");
            myList.add("6");
            myList.add("7");
        }
    }

You can use CopyOnWriteArrayList like below:

    List<String> myList = new CopyOnWriteArrayList<String>();

    myList.add("1");
    myList.add("2");
    myList.add("3");
    myList.add("4");
    myList.add("5");

    Iterator<String> it = myList.iterator();
    while(it.hasNext()){
        String value = it.next();
        System.out.println("List Value:"+value);
        if(value.equals("3")){
            myList.remove("4");
            myList.add("6");
            myList.add("7");
        }
    }
小巷里的女流氓 2024-10-26 14:00:24

这是我使用@idefix第二个解决方案的方法:

private List<TYPE> getFilteredData(List<TYPE> data){                
    List<TYPE> toRemove = new ArrayList<TYPE>(data.size());     
    synchronized(data){
        for(TYPE f : data){
            if([CONDITION]){                        
                toRemove.add(f);
                Log.w(TAG, "Element removed: "+ f);                 
            }
        }
    }                   
    data.removeAll(toRemove);
    return data;        
}

谢谢@idefix +1

This is my method using @idefix second solution:

private List<TYPE> getFilteredData(List<TYPE> data){                
    List<TYPE> toRemove = new ArrayList<TYPE>(data.size());     
    synchronized(data){
        for(TYPE f : data){
            if([CONDITION]){                        
                toRemove.add(f);
                Log.w(TAG, "Element removed: "+ f);                 
            }
        }
    }                   
    data.removeAll(toRemove);
    return data;        
}

Thanks @idefix +1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文