FATAL EXCEPTION:Android中的Thread-92(java.util.ConcurrentModificationException)

时间:2012-02-20 09:45:50

标签: java android arraylist android-canvas

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

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)

我知道它与线程和arraylist有关,但我无法解决问题。请帮助。

2 个答案:

答案 0 :(得分:1)

onTouch()方法正在修改pointsToDraw列表,而onDraw()方法正在访问它。您需要添加同步 - 使用围绕synchronized变量访问权限的pointsToDraw块,例如synchronized(this) { ... access pointsToDraw here ... }

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

答案 1 :(得分:0)

通常情况下,当一个线程忙于迭代Collection时会发生这种情况,而其他线程正在尝试修改它时。但我不确定你的代码中的问题在哪里

一旦检查此链接

java.util.ConcurrentModificationException in Non Multithreaded Program