3

Some times when running my app I get a null pointer when retrieving a value bundled with an intent

setting it in one class

private void start(){
    Intent i = new Intent(this,Tabs.class);
    i.putExtra("helper", checked);
    startActivity(i);
}

checked is a boolean value and is never null

getting it in the other class

private void getExtra(){
    Bundle extras = getIntent().getExtras();
    mExtra = extras.getBoolean("helper");
}

any ideas as to why it would be null sometimes?

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Does this happens when you navigate back to this activity after hitting "Back" button on other activity or via Task Manager? – Ognyan Jul 07 '11 at 15:43
  • hitting the back button at this point in the application would just exit the app since i have the previous activity set as no history – tyczj Jul 07 '11 at 15:51

1 Answers1

7

Use getBooleanExtra() to get the value from intent

Sample Code

boolean mExtra = getIntent().getBooleanExtra("helper", false);
  • 1
    it will not return null, he/she was getting null. also if the data for the key "helper" is present it will return true. if no data is present with the key "helper" it will return false –  Jul 07 '11 at 15:54
  • why wouldn't the key be there? I have a feeling that may be the actual problem where the key is not there sometimes – tyczj Jul 07 '11 at 16:41