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...