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?
There is no string extra to get since it is an
int
. You should haveI'm not sure why you are trying to get a
String
then parse to anint
when it is sent as anint
, assumingposition
is anint
in your firstActivity
. If that is not the case then please explain a little better.Intent Docs