This question already has an answer here:
- Android intent bundle always null? 3 answers
I have some extras sent to a new intent. There it grabs the bundle and tests if it is null
. Every single time it is null
even though I am able to get the values passed and use them.
Can anyone see what is wrong with the if statement?
Intent i = getIntent();
Bundle b = i.getExtras();
int picked = b.getInt("PICK");
int correct = b.getInt("CORR");
type = b.getString("RAND");
if(b == null || !b.containsKey("WELL")) {
Log.v("BUNDLE", "bun is null");
} else {
Log.v("BUNDLE", "Got bun well");
}
EDIT: Heres where the bundle is created.
Intent intent = new Intent(this, app.pack.son.class);
Bundle b = new Bundle();
b.putInt("PICK", pick);
b.putInt("CORR", corr);
b.putString("RAND", "yes");
intent.putExtras(b);
startActivity(intent);
Well, you test
b == null
after using it, which must be wrong. Also are you missing the initialif
?After edits
You don't put "WELL" inside the bundle, so the string "bun is null" is logged. The
b
must be non-null or you would have a crash at your hands. Try changing yourif
sentences to this and report what you get:I change your
if
statement to match the log message. Please run the program and check the log message.I don't think the problem is that your bundle is
null
. It can't be because you'd get aNullPointerException
much sooner.The problem is that your error message is wrong. Change this:
To this:
And the reason why the bundle doesn't contain this key, is because you didn't add it.