gpt4 book ai didi

java - 当我尝试将项目添加到集合时出现 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-02 00:34:29 26 4
gpt4 key购买 nike

我花了一整天的时间试图解决这个问题。我尝试使用迭代?同步,以及许多其他萨满方法,但我一直收到 ConcurrentModificationException。这里是代码。

package com.androidgui.test;

import java.util.ArrayList;
import java.util.ListIterator;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.widget.Toast;

public class CustomView extends SurfaceView implements Callback {
private UpdateThread thread;
private AButton btn;
private AWindow wnd;
private ArrayList<AControl> globalControls;
private Object sync;
public static int posX;
public static int posY;

public CustomView(Context context) {
super(context);
this.getHolder().addCallback(this);
this.LoadResourse();
sync = new Object();
this.globalControls = new ArrayList<AControl>();
btn = new AButton(10, 10,null,AControl.InterfaceImages.Button);
this.globalControls.add(btn);
wnd = new AWindow(10, 10, 200, 100, null);
this.SetDelegates();

}
private void LoadResourse()
{
AControl.InterfaceImages.Button = BitmapFactory.decodeResource(getResources(), R.drawable.button);
}
private void SetDelegates()
{
btn.setEventHandler(new EventHandler() {

@Override
public void ProcessEvent() {
synchronized (sync) {
globalControls.add(wnd);
}
}
});
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh)
{
posX = (this.getWidth()- AControl.InterfaceImages.Button.getWidth()) /2;
posY = (this.getHeight()- AControl.InterfaceImages.Button.getHeight()) /2;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
thread = new UpdateThread(this.getHolder(), this);
thread.setRunning(true);
thread.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
thread.setRunning(false);
boolean _retry=true;
try
{
while(_retry)
{
thread.join();
_retry=false;
}
}catch(Exception ex)
{

}
}
public boolean onTouchEvent(MotionEvent event)
{
synchronized (sync) {
for(AControl item :this.globalControls)
item.onClick(event);
}

return true;
}
public void onDraw(Canvas canvas)
{
synchronized (sync) {
ArrayList<AControl> temp = (ArrayList<AControl>) this.globalControls.clone();
for(AControl item :temp)
item.onDraw(canvas);
}
}

}

最佳答案

您在此处克隆了您的列表:

 public void onDraw(Canvas canvas)
{
synchronized (sync) {
ArrayList<AControl> temp = (ArrayList<AControl>) this.globalControls.clone();
for(AControl item :temp)
item.onDraw(canvas);
}
}

但不在它之前的方法中:

  public boolean onTouchEvent(MotionEvent event)
{
synchronized (sync) {
for(AControl item :this.globalControls)
item.onClick(event);
}

return true;
}

我想说这就是问题所在。但事实上,您没有包含堆栈跟踪,这使得猜测变得更加困难。添加同步并不能解决您的问题,因为问题是您可能在遍历列表时修改列表。

关于java - 当我尝试将项目添加到集合时出现 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8183455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com