Android game loop thread wreck

I am very new to programming and I have a problem with a game I am trying to do.

Almost every time I leave the game, it crashes. After adding: if (canvas! = Null) beforew canvas.drawColor (Color.CYAN); it no longer crashes on exit, but when I go back to play.

Here is the code: (its not my actual game ... OnDraw has nothing but canvas.drawColor (Color.CYAN), which makes it easy)

GameView.java:

package com.example.test.threadtest;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GameView extends SurfaceView {

    private GameLoopThread theGameLoopThread;
    private SurfaceHolder surfaceHolder;


    public GameView(Context context) {
        super(context);
        theGameLoopThread = new GameLoopThread(this);
        surfaceHolder = getHolder();


        surfaceHolder.addCallback(new SurfaceHolder.Callback() {


            public void surfaceDestroyed(SurfaceHolder holder) {

                boolean retry = true;
                theGameLoopThread.setRunning(false);
                while (retry) {
                    try {
                        theGameLoopThread.join();
                        retry = false;
                    } catch (InterruptedException e) {

                    }

                }

            }

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

            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
                // TODO Auto-generated method stub

            }
        });

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (canvas != null)
            canvas.drawColor(Color.CYAN);


    }
}

      

GameLoopThread.java:

package com.example.test.threadtest;



import android.annotation.SuppressLint;
import android.graphics.Canvas;



public class GameLoopThread extends Thread {
    static final long FPS = 20;
    private GameView theView;
    private boolean isRunning = false;

    public GameLoopThread(GameView theView) {
        this.theView = theView;
    }

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

    @SuppressLint("WrongCall") @Override
    public void run() {
        long TPS = 1000 / FPS;
        long startTime, sleepTime;
        while (isRunning) {
            Canvas theCanvas = null;
            startTime = System.currentTimeMillis();
            try {
                theCanvas = theView.getHolder().lockCanvas();
                synchronized (theView.getHolder()) {
                    theView.onDraw(theCanvas);
                }
            } finally {
                if (theCanvas != null) {
                    theView.getHolder().unlockCanvasAndPost(theCanvas);
                }
            }
            sleepTime = TPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    sleep(sleepTime);
                else
                    sleep(10);
            } catch (Exception e) {

            }
        }
    }
}

      

MainAcitvity.java:

package com.example.test.threadtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

      

+3


source to share





All Articles