有致命的错误:theared和无法弄明白

时间:2014-01-12 19:16:02

标签: android fatal-error

我遇到错误:“FATAL EXCEPTION:thread-186”在我对代码的这一部分做了一些更改后:我使用了thread,surfaceHolder,surfaceView

    public void run() {
    long stepPerSecond=1000/FPS;
    long startTime;
    long sleepTime;
    while(isRunning){
        turned=false;
        Canvas c= null;
        startTime=System.currentTimeMillis();
        try{
            prevStep=new Snake(snake);
            c=this.getHolder().lockCanvas();
            synchronized (this.getHolder()) {
                this.onDraw(c);
                }
        }
        catch(Exception e){
        }
        finally{
            if(c!=null&&!turned){
                this.getHolder().unlockCanvasAndPost(c);
                }
            if(turned){
                snake= new Snake(prevStep);
                try{
                    synchronized (this.getHolder()) {
                        this.onDraw(c);
                        }
                }
                catch(Exception e){
                }
                finally{
                    if(c!=null){
                        this.getHolder().unlockCanvasAndPost(c);
                        turned=false;
                        }
                }
        }
        sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
        if(sleepTime>0)
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        else
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
    }

}

这是整个部分:

 package com.example.snake;

import java.util.ArrayList;
import java.util.List;


@SuppressLint("WrongCall")
public class GameView1 extends SurfaceView implements Runnable {

    private boolean isRunning=false;
    private final long FPS=10;

    private Bitmap bmp;
    private SurfaceHolder holder;
    private Thread gamethread;
    //private int x=0;
    //private long clickOnSprite;
     Snake snake;
     Snake prevStep;
     boolean turned;

    private List<Sprite> sprites=new ArrayList<Sprite>();

    public GameView1(Context context) {
        super(context);
        gamethread=new Thread(this);
        holder= getHolder();

        holder.addCallback(new SurfaceHolder.Callback(){

            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
                setRunning(true);
                gamethread.start();

            }

            @Override
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,int arg3) {



            }

            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {

                setRunning(false);

                while(true){
                    try {
                        gamethread.join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }





            }

    });
//  bmp=BitmapFactory.decodeResource(getResources(), R.drawable.image);
        snake=new Snake(20, 50, "RIGHT");

}

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawColor(Color.BLUE);
        snake.onDraw(canvas);
        //canvas.drawBitmap(bmp, x, 10, null);
    }
    /*public boolean onTouchEvent(MotionEvent event){
        if(System.currentTimeMillis()-clickOnSprite>300){
            clickOnSprite=System.currentTimeMillis();
            for (int i=0;i<sprites.size();i++){
                Sprite s=sprites.get(i);
                synchronized (getHolder()) {
                    if(s.isCollition(event.getX(), event.getY())){
                        sprites.remove(s);
                        break;
                    }
                    }
        }
        }
        return true;
    }
    */
    @Override
    public void run() {
        long stepPerSecond=1000/FPS;
        long startTime;
        long sleepTime;
        while(isRunning){
            turned=false;
            Canvas c= null;
            startTime=System.currentTimeMillis();
            try{
                prevStep=new Snake(snake);
                c=this.getHolder().lockCanvas();
                synchronized (this.getHolder()) {
                    this.onDraw(c);
                    }
            }
            catch(Exception e){
            }
            finally{
                if(c!=null&&!turned){
                    this.getHolder().unlockCanvasAndPost(c);
                    }
                if(turned){
                    snake= new Snake(prevStep);
                    try{
                        synchronized (this.getHolder()) {
                            this.onDraw(c);
                            }
                    }
                    catch(Exception e){
                    }
                    finally{
                        if(c!=null){
                            this.getHolder().unlockCanvasAndPost(c);
                            turned=false;
                            }
                    }
            }
            sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
            if(sleepTime>0)
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            else
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }
        }

    }
    public void setRunning(boolean b){
        isRunning=b;
    }

    public boolean isTurned() {
        return turned;
    }

    public void setTurned(boolean turned) {
        this.turned = turned;
    }

}

这里是错误:

01-12 19:03:20.375: E/AndroidRuntime(6088): FATAL EXCEPTION: Thread-186
01-12 19:03:20.375: E/AndroidRuntime(6088): java.lang.IllegalArgumentException
01-12 19:03:20.375: E/AndroidRuntime(6088):     at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
01-12 19:03:20.375: E/AndroidRuntime(6088):     at android.view.Surface.unlockCanvasAndPost(Surface.java:462)
01-12 19:03:20.375: E/AndroidRuntime(6088):     at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:812)
01-12 19:03:20.375: E/AndroidRuntime(6088):     at com.example.snake.GameView1.run(GameView1.java:138)
01-12 19:03:20.375: E/AndroidRuntime(6088):     at java.lang.Thread.run(Thread.java:856)
01-12 19:03:24.245: I/Process(6088): Sending signal. PID: 6088 SIG: 9

我无法弄清楚它是什么 顺便说一句,当我在调试模式时,错误不会发生,当我使用调试模式没有停止

1 个答案:

答案 0 :(得分:0)

如果setTurned(boolean turned)方法在运行循环中间使用true参数调用(之前为false),则会尝试再次解锁画布,导致到那个例外。我建议您切换到一个队列,您可以在run循环的开头添加转换的布尔值并从池中选择项目,并使用适当的同步进行保护(甚至更好的内部同步队列)。

简单地同步setTurned(boolean turned)方法无法解决您的问题。