Android - Force Close - Null Pointer on Canvas?

Posted by user22241 on Game Development See other posts from Game Development or by user22241
Published on 2012-12-17T20:43:15Z Indexed on 2012/12/17 23:14 UTC
Read the original article Hit count: 238

Filed under:
|
|

Please bear with me. I have a very odd problem. Basically, my app so far, has 3 activities (a main splash screen, an 'options/menu' screen and the main app).

If I follow the very specific steps oulined below, I get a 'null pointer exception' in the 2nd activity) and the app force closes......

Here are the steps:

Start the app (a game based on Surfaceview), tap through to the third activity so the game is running, then hit the home key so the game is paused and put to the background, the activity/app is ended through DDMS in the SDK then restarted on the device (all OK so far), now if I hit the back key on the device twice in quick succession, it happens.

All other sequence of events is fine, even to the point of pressing the back key, waiting for the previous activity to show, then hitting back again - all OK. Only when the back key is pressed twice in quick succession following all the above steps does the problem occur.

I'm assuming that the canvas isn't ready as it's showing as 'null' when this happens, but I'm not sure why this is happening as surely it's happening when I'm trying to go back to activity 1, but the logcat shows the error in activity 2.

if I stop the activity running my 'doDraw' method (which referenced the canvas), then all is OK - so I can safely assume it is the canvas causing the problem.

Also, if I skip my first activity (which is a very basic full-screen button which just displays a splashscreen and waits for the user to tap the screen), and make my 2nd activity the launch activity, again, it is OK.

this is the part of the code that I think is probably relevant:

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


    vheight = this.getHeight();         
    vwidth = this.getWidth();           

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    vheight = this.getHeight();         
    vwidth = this.getWidth();           
    this.viewWidth = vwidth;
    this.viewHeight = vheight;

    if (runthread==false){

    if (preThread.getState()==Thread.State.TERMINATED){

    preThread = new OptionsThread(thisholder, thiscontext, thishandler);  

}       
    preThread.setRunning(true);
    preThread.start();}

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    preThread.setRunning(false);  //Stop the loop
    boolean retry = true;         //Stop the thread
    while (retry) {
         try {
            preThread.join();
         retry = false;
         } catch (InterruptedException e) {
         }
         }

Thank you all for any help you can offer

© Game Development or respective owner

Related posts about android

Related posts about game-design