Intent Bundle returns Null every time?

I have additional additions aimed at a new intention. There he grabs a package and tests if it is null. Each time it is null, although I can 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 package 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);
+1
source share
3 answers

I do not think the problem is your package null. This cannot be, because you will receive soon NullPointerException.

The problem is that your error message is incorrect. Change this:

if(b == null || !b.containsKey("WELL")) {
    Log.v("BUNDLE", "bun is null");
} else {
    // ...
}

For this:

if (!b.containsKey("WELL")) {
    Log.v("BUNDLE", "bundle does not contain key WELL");
} else {
    // ...
}

And the reason why the package does not contain this key is because you did not add it.

+5
source

, b == null , . if?


"WELL" , "bun is null". b , . if , :

if (b == null) {
    Log.v("BUNDLE", "bun is null");
} else if (!b.containsKey("WELL")) {
    Log.v("BUNDLE", "bun doesn't contain WELL");
else {
    Log.v("BUNDLE", "Got bun well");
}
0

I am modifying your statement ifaccording to the log message. Run the program and check the log message.

if(b == null) {
    Log.v("BUNDLE", "bun is null");
} else {
    if (b.containsKey("WELL")) {
        Log.v("BUNDLE", "Got bun well");
    }
}
0
source

All Articles