gpt4 book ai didi

java - 添加了一个选项菜单,现在进程停止

转载 作者:行者123 更新时间:2023-11-30 04:47:14 25 4
gpt4 key购买 nike

我一直在开发一个应用程序,我添加了一个选项菜单。在添加菜单之前,一切都运行良好,但在添加之后它不会运行,进程就停止了。我尝试删除菜单但没有用,它仍然停止。在 logcat 中它说有一个空指针异常,但我完全不知道为什么会这样。

我的 numbers_menu xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/quit"
android:title="@string/quit_str" ></item>

<item android:id="@+id/instructions"
android:title="@string/instructions_str"></item>

</menu>

方法:

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.numbers_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.quit:
quit();
return true;
case R.id.instructions:
return true;
default:
return super.onOptionsItemSelected(item);
}
}

这是导致空指针异常的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

play_game();

}

public void play_game() {

//get some numbers
rolled1 = roll();
Log.d(TAG, "rolled1 is up");
rolled2 = roll();
Log.d(TAG, "rolled2 is up");

switch(rolled1) {
case 1:
rolled_id_1 = 2131034114;
break;
case 2:
rolled_id_1 = 2131034115;
break;
case 3:
rolled_id_1 = 2131034116;
break;
case 4:
rolled_id_1 = 2131034117;
break;
case 5:
rolled_id_1 = 2131034119;
break;
case 6:
rolled_id_1 = 2131034120;
break;
}

Log.d(TAG, "rolled_id_1 is up with the value " + Integer.toString(rolled_id_1));

switch(rolled2) {
case 1:
rolled_id_2 = 2131034114;
break;
case 2:
rolled_id_2 = 2131034115;
break;
case 3:
rolled_id_2 = 2131034116;
break;
case 4:
rolled_id_2 = 2131034117;
break;
case 5:
rolled_id_2 = 2131034119;
break;
case 6:
rolled_id_2 = 2131034120;
break;
}

Log.d(TAG, "rolled_id_2 is up with the value " + Integer.toString(rolled_id_2));

switch(rolled1 + rolled2) {
case 1:
rolled_sum = 2131034114;
break;
case 2:
rolled_sum = 2131034115;
break;
case 3:
rolled_sum = 2131034116;
break;
case 4:
rolled_sum = 2131034117;
break;
case 5:
rolled_sum = 2131034119;
break;
case 6:
rolled_sum = 2131034120;
break;
case 7:
rolled_sum = 2131034121;
break;
case 8:
rolled_sum = 2131034122;
break;
case 9:
rolled_sum = 2131034124;
break;
case 10:
rolled_sum = 2131034125;
break;
case 11:
rolled_sum = 2131034126;
break;
case 12:
rolled_sum = 2131034127;
break;
}

Log.d(TAG, "rolled_id_sum is up with the value " + Integer.toString(rolled_sum));

if(lose(rolled_id_1, rolled_id_2, rolled_sum)) {
dieShow(rolled1, rolled2);
setContentView(R.layout.main);
play_game();
}
else {
disable_flag = 0;
needDisable(rolled_id_1, rolled_id_2);
dieShow(rolled1, rolled2);
}



}

public boolean lose(int rolled_id_1, int rolled_id_2, int rolled_sum) {
//check loss
Button button_check_loss_1 = (Button)findViewById(rolled_id_1);
Log.d(TAG, "getting past first button assignment");
Button button_check_loss_2 = (Button)findViewById(rolled_id_2);
Log.d(TAG, "getting past second button assignment");
Button button_check_loss_3 = (Button)findViewById(rolled_sum);
Log.d(TAG, "getting past third button assignment");

if((!button_check_loss_1.isClickable() || !button_check_loss_2.isClickable()) && !button_check_loss_3.isClickable()) {
Toast loseText = Toast.makeText(this, "you lose1", Toast.LENGTH_LONG);
loseText.show();
return true;
}

if(!button_check_loss_1.isClickable() && !button_check_loss_2.isClickable() && !button_check_loss_3.isClickable()) {
Toast loseText = Toast.makeText(this, "you lose2", Toast.LENGTH_LONG);
loseText.show();
return true;

}

if(rolled_id_1 == rolled_id_2) {
if ((!button_check_loss_1.isClickable() || !button_check_loss_2.isClickable()) && !button_check_loss_3.isClickable()) {
Toast loseText = Toast.makeText(this, "you lose3", Toast.LENGTH_LONG);
loseText.show();
return true;
}
}

return false;

}

错误发生在:

if((!button_check_loss_1.isClickable() || !button_check_loss_2.isClickable()) && !button_check_loss_3.isClickable())

最佳答案

case 1:
rolled_id_1 = 2131034114;
break;

让我猜猜...2131034114 是您从/gen/.../R.java 获取的按钮的 int Id - 我猜对了吗?

如果是这样,这真的不是一个好的做事方式。不能保证生成的 R.java 文件的内容是不变的 - 通过添加选项菜单,所有(或部分)按钮的 int ID 可能已更改。

您收到 NullPointerException 的原因可能是这段代码...

Button button_check_loss_1 = (Button)findViewById(rolled_id_1);
Log.d(TAG, "getting past first button assignment");
Button button_check_loss_2 = (Button)findViewById(rolled_id_2);
Log.d(TAG, "getting past second button assignment");
Button button_check_loss_3 = (Button)findViewById(rolled_sum);
Log.d(TAG, "getting past third button assignment");

...可能找不到其中一个(或所有)按钮 ID。分配给 rolled_id_1 时需要使用 R.id.button1(例如)。

关于java - 添加了一个选项菜单,现在进程停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4567332/

25 4 0