0

I want to inflate a layout when the player is death (a game over layout). I tried it like this:

View GameOverDialog;

LayoutInflater myInflater = (LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
GameOverDialog = myInflater.inflate(R.layout.lose, null, false);
rlGame.addView(GameOverDialog);
GameOverDialog.setVisibility(View.GONE);

And when the player dies:

private void lose(){
    GameOverDialog.setVisibility(View.VISIBLE);
}

I also use a Handler so I can send a message when the player dies:

final static int DEATH = 0, LOSE = 1;

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == DEATH) {
            //play music
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    Message msg = handler.obtainMessage();
                    msg.what = LOSE;
                    handler.sendMessage(msg);
                }
            }, 1000);
        }
        if (msg.what == LOSE) {
            lose();
        }
        super.handleMessage(msg);
    }
};

But when the Handler calls the lose() method, the application crashes and I get a NullPointerException. I hope someone can help me :).

Thanks in advance!
Joeri.

EDIT

Full class:

public class Game extends Activity {

final static int DEATH = 0, LOSE = 1;
RelativeLayout rlGame;
GamePanel game_panel;
private int ScreenWidth, ScreenHeight;
View GameOverDialog;
ImageView retry, quit;
TextView textRetry, textQuit;

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == DEATH) {
            //play music
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    Message msg = handler.obtainMessage();
                    msg.what = LOSE;
                    handler.sendMessage(msg);
                }
            }, 1000);
        }
        if (msg.what == LOSE) {
            lose();
        }
        super.handleMessage(msg);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    rlGame = (RelativeLayout) findViewById(R.id.rlGame);
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    final int height = dm.heightPixels;
    final int width = dm.widthPixels;

    game_panel = new GamePanel(getApplicationContext(), width, height, rlGame);
    rlGame.addView(game_panel);

    LayoutInflater myInflater = (LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
    GameOverDialog = myInflater.inflate(R.layout.lose, null, false);
    rlGame.addView(GameOverDialog);
    retry = (ImageView) GameOverDialog.findViewById(R.id.retry);
    retry.setOnTouchListener(new TouchButton(retry, textRetry));
    quit = (ImageView) GameOverDialog.findViewById(R.id.quit);
    quit.setOnTouchListener(new TouchButton(quit, textQuit));
    textRetry = (TextView)GameOverDialog.findViewById(R.id.textRetry);
    textQuit = (TextView)GameOverDialog.findViewById(R.id.textQuit);
    GameOverDialog.setVisibility(View.GONE);
}

@Override
public void onBackPressed() {
}

@Override
protected void onStop() {
    super.onStop();
}

private void lose() {
    GameOverDialog.setVisibility(View.VISIBLE);
}

}

I also tried to set visibility to VISIBLE in the onBackPressed method, this worked, I don't know why, but in the handler goes something wrong I think...

Joeri
  • 85
  • 7
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Mar 08 '15 at 19:15
  • is `GameOverDialog` stored in the class variables? Please show the full class – engineercoding Mar 08 '15 at 19:16
  • try calling lose() inside postDelayed() – jazdev Mar 08 '15 at 19:16
  • I tried to call lose() inside postDelayed, but that didn't work – Joeri Mar 08 '15 at 19:21
  • Post the full error message and mark the line where it occurs. – Simas Mar 08 '15 at 19:31
  • I surrounded the GameOverDialog.setVisibility(View.VISIBLE); with an if statement: if(GameOverDialog != null) GameOverDialog.setVisibility(View.VISIBLE); else Log.d("GameOverDialog", "= NULL"); and I saw that GameOverDialog = null – Joeri Mar 08 '15 at 19:33
  • Full error: 03-08 20:38:15.456: E/AndroidRuntime(22347): FATAL EXCEPTION: main 03-08 20:38:15.456: E/AndroidRuntime(22347): Process: com.jakkerman.appgame, PID: 22347 03-08 20:38:15.456: E/AndroidRuntime(22347): java.lang.NullPointerException 03-08 20:38:15.456: E/AndroidRuntime(22347): at com.jakkerman.appgame.Game.lose(Game.java:104) 03-08 20:38:15.456: E/AndroidRuntime(22347): at com.jakkerman.appgame.Game.access$0(Game.java:102) 03-08 20:38:15.456: E/AndroidRuntime(22347): at com.jakkerman.appgame.Game$1.handleMessage(Game.java:45) – Joeri Mar 08 '15 at 19:46
  • and more, but I don't think it is necessary: at android.os.Handler.dispatchMessage(Handler.java:102) 03-08 20:38:15.456: E/AndroidRuntime(22347): at android.os.Looper.loop(Looper.java:146) 03-08 20:38:15.456: E/AndroidRuntime(22347): at android.app.ActivityThread.main(ActivityThread.java:5602) 03-08 20:38:15.456: E/AndroidRuntime(22347): at java.lang.reflect.Method.invokeNative(Native Method) 03-08 20:38:15.456: E/AndroidRuntime(22347): at java.lang.reflect.Method.invoke(Method.java:515) – Joeri Mar 08 '15 at 19:48
  • Necessary stuff: **03-08 20:49:07.366: E/AndroidRuntime(22641): java.lang.NullPointerException 03-08 20:49:07.366: E/AndroidRuntime(22641): at com.jakkerman.appgame.Game.lose(Game.java:104) 03-08 20:49:07.366: E/AndroidRuntime(22641): at com.jakkerman.appgame.Game.access$0(Game.java:102) 03-08 20:49:07.366: E/AndroidRuntime(22641): at com.jakkerman.appgame.Game$1.handleMessage(Game.java:45)** – Joeri Mar 08 '15 at 19:50

1 Answers1

1

Avoid using getApplicationContext() when you don't have to:

game_panel = new GamePanel(this, width, height, rlGame);
rlGame.addView(game_panel);

LayoutInflater myInflater = (LayoutInflater)
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Edit:

As per your comment (next time, please just update your question), it seems that the handleMessage is called before onCreate where your variable is set. So set your listener after the field is initialized:

Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    // Init field
    GameOverDialog = myInflater.inflate(R.layout.lose, null, false);
    // Set the handler
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == DEATH) {
                //play music
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = handler.obtainMessage();
                        msg.what = LOSE;
                        handler.sendMessage(msg);
                    }
                }, 1000);
            }
            if (msg.what == LOSE) {
                lose();
            }
            super.handleMessage(msg);
        }
    };
    ...
}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • I dont get the error anymore but now I get another error at the location where I set a message for the handler (when the player dies): Message msg = gamepanel.game.handler.obtainMessage (); msg.what = 0; gamepanel.game.handler.sendMessage (msg); It is btw a nullpointerexception again – Joeri Mar 08 '15 at 20:03
  • @Joeri you're calling the handler too early -- activity `Game` is not yet ready. Seems like a whole new problem here so might as well ask another question. – Simas Mar 08 '15 at 20:06
  • But Game is the first activity then I call the GamePanel and in the GamePanel I call the Player and there (in the Player class) I have the code above with the message. In the Game activity I initialized the handler before the GamePanel class so it should be ready... – Joeri Mar 08 '15 at 20:17
  • should I ask a new question? – Joeri Mar 08 '15 at 20:39