获取线程已经在这个类上启动错误?

时间:2011-11-15 06:12:07

标签: java android multithreading

这是我的类,我实现了在画布上绘制的线程。

    package com.example.drawing;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.drawing.DrawingActivity;

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
    public static Boolean _run;
    public static DrawThread thread;
    public Canvas canvas = null;
    private CommandManager commandManager;
    //private Bitmap myBitmap;
    private Bitmap mBitmap;

    public DrawingSurface(Context context, AttributeSet attrs) {
        super(context, attrs);

        getHolder().addCallback(this);

        commandManager = new CommandManager();
        thread = new DrawThread(getHolder());
    }

    class DrawThread extends Thread{
        private SurfaceHolder mSurfaceHolder;

        public DrawThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;

        }

        public void setRunning(boolean run) {
            _run = run;
        }

        @Override
        public void run() {
            //Canvas canvas = null;
            while (_run){

                try{

                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }

                    final Canvas c = new Canvas (mBitmap);
                    //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(Color.WHITE); 

//                    Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                      canvas.drawBitmap (kangoo, 0,  200,null);

//                  works for logo                  
//                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                  c.drawBitmap (kangoo, 0,  200,null);

                    if(!(DrawingActivity.imagePath==null)){
                        c.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
                    }
                    commandManager.executeAll(c);
                    canvas.drawBitmap (mBitmap, 0,  0,null);

                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    public void addDrawingPath (DrawingPath drawingPath){
        commandManager.addCommand(drawingPath);
    }

    public boolean hasMoreRedo(){
        return commandManager.hasMoreRedo();
    }

    public void redo(){
        commandManager.redo();
    }

    public void undo(){
        commandManager.undo();
    }

    public boolean hasMoreUndo(){
        return commandManager.hasMoreRedo();
    }

    public Bitmap getBitmap(){
        Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.resize_drawing_logo);
        Bitmap myBitmap = addLogo(mBitmap, kangoo);
        return myBitmap;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {
        // TODO Auto-generated method stub
        mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
            //thread.setRunning(true); // Edited By Shreyash
            _run=true; //
            thread.start();
             // error at this line 
//          if(!thread.isAlive())            
//               thread.start(); 
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        //thread.setRunning(false); //Edited By Shreyash
        _run = false;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
    public static Bitmap addLogo(Bitmap mainImage, Bitmap logoImage) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 

        Bitmap finalImage = null; 
        int width, height = 0; 
        width = mainImage.getWidth(); 
        height = mainImage.getHeight(); 
        finalImage = Bitmap.createBitmap(width, height, mainImage.getConfig()); 
        Canvas canvas = new Canvas(finalImage); 
        canvas.drawBitmap(mainImage, 0,0,null);
        canvas.drawBitmap(logoImage, canvas.getWidth()-logoImage.getWidth() ,canvas.getHeight()-logoImage.getHeight() ,null);

        return finalImage; 
    }


}

我正在为另一个活动创建此类的Object并进行绘制。 现在,当我要进行另一项活动,然后回到此活动时,就会发生错误。

错误日志:

11-15 11:26:40.738: ERROR/AndroidRuntime(668): FATAL EXCEPTION: main
11-15 11:26:40.738: ERROR/AndroidRuntime(668): java.lang.IllegalThreadStateException: Thread already started.
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at java.lang.Thread.start(Thread.java:1322)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at com.example.drawing.DrawingSurface.surfaceCreated(DrawingSurface.java:119)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.os.Looper.loop(Looper.java:123)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at java.lang.reflect.Method.invoke(Method.java:521)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 11:26:40.738: ERROR/AndroidRuntime(668):     at dalvik.system.NativeStart.main(Native Method)
11-15 11:26:40.768: WARN/ActivityManager(60):   Force finishing activity com.example.drawing/.DrawingActivity
11-15 11:26:41.338: WARN/ActivityManager(60): Activity pause timeout for HistoryRecord{450073d0 com.example.drawing/.DrawingActivity}

我不知道为什么我会收到此错误。 所以请帮我处理这个帖子。 感谢。

3 个答案:

答案 0 :(得分:2)

嗯,就像它说的那样:你在已经启动的线程上调用start()。想要在一段代码中创建一个线程,然后在另一段代码中启动它,非常不寻常......如果你能把两者放在一起,就不会发生这个问题。

(顺便说一句,看起来你的线程基本上是紧密循环的,这对我来说似乎不是一个好主意,但这是另一回事。)

答案 1 :(得分:1)

在我的情况下,我通过初始化新线程来修复它:

public void surfaceCreated(SurfaceHolder holder) {
    gameThread = new MyThread(getHolder(), _context, this);
    gameThread.setRunning(true);
    gameThread.start();
}

答案 2 :(得分:0)

修改surfaceDestroyed()方法:

    thread.interrupt();
    thread.join();

你需要包围代码

    thread.start();
使用try-catch的surfaceCreated()方法中的

。 Interrupt()方法抛出中断异常

对我来说效果很好。