I’m trying to pass 2 variables through a couple of Android Activities. One of them keeps turning up as null on the last page:
The first Activity:
Intent intent= new Intent(RoundOptionActivity.this, MoveOptionActivity.class);
intent.putExtra("numRounds", "5");
startActivity(intent);
The second Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
.........
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds);
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);
(Note that at this point I printed numRounds in LogCat and it was set to the right number, and not null)
The Third Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
playerChoice = Integer.parseInt(extras.getString("playerChoice"));
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
At this point, the application crashes at the line where I try to parse numRounds to an integer, with a NumberFormatException complaining that it can’t parse a null value. There’s never a problem with playerChoice, only numRounds. I’ve tried handling numRounds the exact same way as playerChoice, but nothing seems to work. What’s going on? D:
You have to use
extras.getInt("numRounds");because in second
Activityyou added toputExtraint value: