Search code examples
androidandroid-intentnullpointerexceptionextra

Null pointer when getting intent extra


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?


Solution

  • Use getBooleanExtra() to get the value from intent

    Sample Code

    boolean mExtra = getIntent().getBooleanExtra("helper", false);