[英]Inflate Layout NullPointerException
我想在玩家死亡時給布局充氣(游戲超出布局)。 我這樣嘗試過:
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);
玩家死亡時:
private void lose(){
GameOverDialog.setVisibility(View.VISIBLE);
}
我還使用處理程序,以便玩家死亡時可以發送消息:
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);
}
};
但是,當處理程序調用Los()方法時,應用程序崩潰了,我得到了NullPointerException。 我希望有一個人可以幫助我 :)。
提前致謝!
Joeri。
編輯
全班:
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);
}
}
我還嘗試在onBackPressed方法中將可見性設置為VISIBLE,這行得通,我不知道為什么,但是我認為在處理程序中出了點問題...
避免在不需要時使用getApplicationContext()
:
game_panel = new GamePanel(this, width, height, rlGame);
rlGame.addView(game_panel);
LayoutInflater myInflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
編輯:
根據您的評論(下一次,請更新您的問題),似乎在設置變量的onCreate
之前調用了handleMessage。 因此,在字段初始化之后設置您的監聽器:
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);
}
};
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.