错误,如果隐藏画布游戏

时间:2015-09-26 19:11:08

标签: java android multithreading canvas

public class GameView extends SurfaceView
{
    private GameThread mThread;
    SurfaceHolder holder;
    Bitmap fon = BitmapFactory.decodeResource(getResources(), R.drawable.fon);
    ArrayList<Integer> lasers = new ArrayList<Integer>();
    ArrayList<Integer> coordYlasers = new ArrayList<Integer>();
    ArrayList<Integer> coordXlasers = new ArrayList<Integer>();
    Bitmap laser = BitmapFactory.decodeResource(getResources(), R.drawable.laser);
    int touchX = 0;
    int touchY = 0;
    int coordX = 0;
    int coordY = 600;
    int _coordX = 0;
    private boolean running = false;
    ArrayList<Bitmap> blocks = new ArrayList<Bitmap>();
    public class GameThread extends Thread
    {
        private GameView view;

        public GameThread(GameView view)
        {
            this.view = view;
        }

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

        public void run()
        {
            int i = 0;
            while (running)
            {
                Canvas canvas = null;
                try
                {
                    canvas = view.getHolder().lockCanvas();
                    synchronized (view.getHolder())
                    {
                        onDraw(canvas);
                    }

                }
                catch (Exception e) { }
                finally
                {
                    if (canvas != null)
                    {
                        view.getHolder().unlockCanvasAndPost(canvas);
                    }
                }
            }
        }
    }
    public GameView(Context context)
    {
        super(context);

        mThread = new GameThread(this);
        getHolder().addCallback(new SurfaceHolder.Callback() {
            public void surfaceDestroyed(SurfaceHolder holder) {
                boolean retry = true;
                mThread.setRunning(false);
                while (retry) {
                    try {
                        mThread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                    }
                }
            }

            public void surfaceCreated(SurfaceHolder holder) {
                mThread.setRunning(true);
                mThread.start();

            }

            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
        });
    }
    public boolean onTouchEvent(MotionEvent event)
    {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            _coordX = coordX;
            X = (int) event.getX();
            Y = (int) event.getY();

        }
        if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            touchX = ((int) event.getX());
            touchY = (int) event.getY();
            //if (Math.abs(touchX - X) > 10){
            coordX = _coordX + (touchX - X);
            invalidate();

            //}
        }
        if(event.getAction() == MotionEvent.ACTION_UP)
        {
            _coordX = coordX;
            Y = touchY;
        }
        return true;
    }
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(20);

        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(fon, 0, 0, null);
        canvas.drawBitmap(player, coordX, coordY, null);
        canvas.drawBitmap(laser, coordLaserX, coordLaserY, null);
        for (int i = 1; i<coordYlasers.size(); ++i){
            canvas.drawBitmap(laser, coordXlasers.get(i), coordYlasers.get(i), null);
        }
        canvas.drawText("" + V, 10, 25, paint);
    }

}

应用程序运行良好,但.... 如果我隐藏应用,游戏会崩溃。请帮忙。

Logcat错误。

  

5553-5553 / com.example.thetronuo.pregame E /AndroidRuntime﹕致命   例外:主要       java.lang.IllegalThreadStateException:线程已经启动。               在java.lang.Thread.start(Thread.java:1045)               在com.example.thetronuo.pregame.GameView $ 1.surfaceCreated(GameView.java:161)               在android.view.SurfaceView.updateWindow(SurfaceView.java:569)               在android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:231)               在android.view.View.dispatchWindowVisibilityChanged(View.java:7618)               在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)               在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)               在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)               在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1211)               在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)               在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4356)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)               在android.view.Choreographer.doCallbacks(Choreographer.java:562)               在android.view.Choreographer.doFrame(Choreographer.java:532)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)               在android.os.Handler.handleCallback(Handler.java:725)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:5099)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:803)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)               at dalvik.system.NativeStart.main(Native Method)   这是活动。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(new GameView(this));
}

“wait()” - 未处理的异常。
在哪里写“resumeThread()”?

public class GameThread extends Thread
        {
            private GameView view;
            private boolean pause = false;

            private synchronized void check(){
                while(pause){
                    wait();
                }
            }

            public void pauseThread(){
                pause = true;
            }

            public void resumeThread(){
                pause = false;
                notify();
            }
            public GameThread(GameView view)
            {
                this.view = view;
            }

1 个答案:

答案 0 :(得分:0)

我想我遇到了同样的问题。但我修好了。 把它放在你的Thread类中:

private boolean pause = false;

private synchronized void check(){
while(pause){
wait();
}
}

public void pauseThread(){
pause = true;
}

public void resumeThread(){
pause = false;
notify();
}

当调用onSurfaceDestroyed时,您需要使用您创建的方法暂停线程:pauseThread();

也调用方法check();在while循环中,所以:

while(true){
 check();
... other code ...
}

并在activity类中创建一个onResume()方法,因此每当用户返回应用程序时,它都会自动调用此方法,并将此代码放入方法中:

resumeThread();

我希望这会对你有所帮助,我不确定你是否需要所有代码,因为我现在不在家,如果它不起作用,请在家里看看和编辑。