gpt4 book ai didi

java - Android 中的致命异常 : Thread-92 (java. util.ConcurrentModificationException)

转载 作者:行者123 更新时间:2023-12-01 15:32:52 27 4
gpt4 key购买 nike

我目前正在开发 Android 中的应用程序。首先,我想使用 android.graphics.Path 在 Canvas 上绘图。这是我编写的代码(可能存在某些未使用的导入和注释代码行,因为它不是我的完整代码,只是相关部分):

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


public class HRCanvas extends Activity implements OnTouchListener{

DrawPanel dp;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
private Paint mPaint;
Path path;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dp = new DrawPanel(this);
dp.setOnTouchListener(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(30);

FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
fl.addView(dp);
//fl.addView(tv1);
//fl.addView(b1);
//fl.addView(b2);
//fl.addView(b3);
//fl.addView(ll);
setContentView(fl);
//setContentView(dp);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
dp.pause();
}



@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
dp.resume();
}



public class DrawPanel extends SurfaceView implements Runnable{

Thread t = null;
SurfaceHolder holder;
boolean isItOk = false ;

public DrawPanel(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}

@Override
public void run() {
// TODO Auto-generated method stub
while( isItOk == true){

if(!holder.getSurface().isValid()){
continue;
}

Canvas c = holder.lockCanvas();
c.drawARGB(255, 0, 0, 0);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
for (Path path : pointsToDraw) {
canvas.drawPath(path, mPaint);
}
}

public void pause(){
isItOk = false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}

public void resume(){
isItOk = true;
t = new Thread(this);
t.start();

}



}


@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
if(me.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(me.getX(), me.getY());
//path.lineTo(me.getX(), me.getY());
pointsToDraw.add(path);
}else if(me.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(me.getX(), me.getY());
}else if(me.getAction() == MotionEvent.ACTION_UP){
//path.lineTo(me.getX(), me.getY());
}

return true;

}

}

代码运行良好一段时间,然后我收到应用程序必须关闭的错误(在我的模拟器上)。以下是日志内容:

01-31 22:03:20.988: W/dalvikvm(760): threadid=11: thread exiting with uncaught exception (group=0x409c01f8)
01-31 22:03:20.997: E/AndroidRuntime(760): FATAL EXCEPTION: Thread-92
01-31 22:03:20.997: E/AndroidRuntime(760): java.util.ConcurrentModificationException
01-31 22:03:20.997: E/AndroidRuntime(760): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
01-31 22:03:20.997: E/AndroidRuntime(760): at learn.myandroidapp.hr.HRCanvas$DrawPanel.onDraw(HRCanvas.java:201)
01-31 22:03:20.997: E/AndroidRuntime(760): at learn.myandroidapp.hr.HRCanvas$DrawPanel.run(HRCanvas.java:192)
01-31 22:03:20.997: E/AndroidRuntime(760): at java.lang.Thread.run(Thread.java:856)

我知道这与线程和数组列表有关,但我无法解决这个问题。请帮忙。

最佳答案

看起来,onTouch() 方法正在修改 pointsToDraw 列表,而 onDraw() 方法正在访问它。您需要添加同步 - 在访问 pointsToDraw 变量时使用 synchronized block ,例如synchronized(this) { ... 访问点ToDraw 在这里 ... }.

请参阅此链接以获取一些示例:http://www.javamex.com/tutorials/synchronization_concurrency_synchronized1.shtml

关于java - Android 中的致命异常 : Thread-92 (java. util.ConcurrentModificationException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359041/

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