开发者

Added a options menu, now the process stops

I have been working on an application and I added a options menu. Prior to adding the menu everything ran fine, after adding however it will not run, the process just stops. I tried removing the menu and that did not work, it still stops. In logcat it says that there is a null pointer exception, but I cannot figure out at all why this is happening.

my numbers_menu xml file:

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

methods:

   @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);
        }
    }

Here is the code leading up the the null pointer exception:

@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 t开发者_C百科hird 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;

    }

The error occurs at:

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


case 1:
    rolled_id_1 = 2131034114;
    break;

Let me guess...2131034114 is the int Id of a button that you have taken from /gen/.../R.java - am I correct?

If so this is REALLY NOT a good way of doing things. The contents of the generated R.java file are not guaranteed to be constant - by adding the options menu, the chances are the int Ids of all (or some) of the Buttons have changed.

The reason you're getting a NullPointerException is likely to be that this code...

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");

...probably can't find one (or all) of those Button Ids. You need to use R.id.button1 (for example) when assigning to rolled_id_1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜