23

Android でコードを同期するという概念に欠けているものがあります。

シナリオ

画面には常に3つのアイテムが描かれています。各画像は ArrayList (lstGraphics) に格納されます。この目的のために、SurfaceView を使用します。ユーザーが画像をタップすると、画像の市場が削除され、新しい市場が追加されます。

コードサンプル:

アニメーション非表示スレッド

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

最初に見えるように、私は 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()) {
            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)

どんな助けでも大歓迎です。ありがとうございました。

4

4 に答える 4

78

あなたの問題は、グラフィックとリストを追加する物理メソッドにあります

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

for(Graphic graphic : lstGraphics)との組み合わせによりlst.Graphics.remove(graphic);ConcurrentModificationException が発生します。これは、リストを実行していると同時に変更を試みているためです。

これまでのところ、私は2つの解決策を知っています:

  1. 利用可能な場合は代わりに Iterator を使用します (これまで Android 用にコーディングされたことはありません)。

    while (iter.hasNext) {
        if (physicsCondition) iter.remove();
    }
    
  2. 2番目のリストを使用して要素を保存し、後でそれらを削除します

    List<GraphicsItem> toRemove = new ....
    for (Graphic graphic : lstGraphics) {
        if (physicsCondition) {
            toRemove.add(graphic);
        }
    }
    lstGraphics.removeAll(toRemove);
    
于 2011-03-01T08:33:06.027 に答える
9

@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);
        }
    }
}
于 2011-03-01T08:59:08.147 に答える
4

以下のように 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");
        }
    }
于 2015-06-28T17:09:26.493 に答える
1

これは、@idefix 2 番目のソリューションを使用した私の方法です。

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

于 2014-05-09T00:00:17.157 に答える