1

I made a 2D android game for android and the game works perfectly fine but upon reopening it after closing, the game crashes once (Unfortunately, game has stopped working appears) before working again. This only occurs on newer devices, as I tested it on an API level 8 smaller screen and this does not happen.

I suspect this has something to do with how I use the Bitmap resources for the game. I do not release the resources anywhere in the code, is this something that I should be doing or does the garbage collector automatically handle it?

The game reopens fine on all emulator devices except for the occasional

Exception locking surface
java.lang.IllegalStateException: Surface was already locked

that is thrown on reopen. I do not know the fix for this but it happens very rarely.

Here is the code in the MainThread.java class

while (running) {
    startTime = System.nanoTime();
    canvas = null;

try {
 canvas = this.surfaceHolder.lockCanvas();
        synchronized (surfaceHolder) {
            // update game state
            this.gamePanel.update();
            // draws the canvas on the panel
            this.gamePanel.draw(canvas);
        }

    } catch(Exception e){e.printStackTrace();}finally {
        // in case of an exception the surface is not left in
        // an inconsistent state
        if (canvas != null) {
            try {

                surfaceHolder.unlockCanvasAndPost(canvas);

            }
            catch(Exception e){e.printStackTrace();}
        }
}

method that starts the thread:

@Override
public void surfaceCreated(SurfaceHolder holder) {

    player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.helicopter),65,25,3);


    bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));

    smoke = new ArrayList<Smokepuff>();
    missiles = new ArrayList<Missile>();
    botborder = new ArrayList<BotBorder>();
    topborder = new ArrayList<TopBorder>();
    explosions = new ArrayList<Explosion>();


    smokeStartTime = System.nanoTime();
    missileStartTime = System.nanoTime();

    thread.setRunning(true);
    thread.start();

}

and method that shuts the thread down:

@Override
public void surfaceDestroyed(SurfaceHolder holder)
    {

        boolean retry = true;
        while (retry) {
            try {
                thread.setRunning(false);
                thread.join();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            retry = false;
        }


    }

Any help would be greatly appreciated!

6
  • 1
    What does logcat say when it crashes?
    – user253751
    Mar 5, 2015 at 5:14
  • most of the time the game reopens fine on the emulator except for occasional Exception locking surface java.lang.IllegalStateException: Surface was already locked is spammed into the logcat Mar 5, 2015 at 5:23
  • What does the logcat say when it crashes?
    – user253751
    Mar 5, 2015 at 5:23
  • Add your code please! Especially for your draw functions!
    – T D Nguyen
    Mar 5, 2015 at 5:39
  • 1
    The while loop in surfaceDestroyed() will only ever execute once because you set retry to false no matter what. move retry=false; to the end of the try{} block. Also possibly add a counter so that you only retry a certain number of times maximum to prevent an infinite loop.
    – samgak
    Mar 5, 2015 at 6:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.