AngryOliver
AngryOliver

Reputation: 397

Intent's extra is always null

I'm trying to pass an int variable to another activity:

From the current activity:

Intent intent = new Intent(getApplicationContext(),PlayActivity.class);
intent.putExtra("position", position);
startActivity(intent);

At PlayActivity.onCreate:

Intent intent = getIntent();
String position = intent.getStringExtra("position");
int index = Integer.parseInt(position);

Problem is, position is always null (and parseInt() throws exception).
Why?

Upvotes: 0

Views: 206

Answers (1)

codeMagic
codeMagic

Reputation: 44571

There is no string extra to get since it is an int. You should have

Intent intent = getIntent();
int position = intent.getIntExtra("position");

I'm not sure why you are trying to get a String then parse to an int when it is sent as an int, assuming position is an int in your first Activity. If that is not the case then please explain a little better.

Intent Docs

Upvotes: 5

Related Questions