I am trying to pass data between two activities in my android application however when
i try the run the on click method that sends the data the app crashes.
This is the code of the activity that works out my calculation is trying to send it to another activity called result. The variable output I am trying to send is a double.
Intent myIntent = new Intent(BMIMetric.this, result.class);
BMIMetric.this.startActivity(myIntent);
myIntent.putExtra("key", output);
Then on the results page I am trying to take the variable with this code
Intent myIntent = getIntent();
double output = (Double) getIntent().getExtras().get("Key");
First, you have an order issue (edited code):
You need to set extras before starting the new Activity.
Then in your other Activity do:
getDoubleExtra()seems like a better fit since you’re assigning to a primitive data type.Also, as Blumer mentioned,
"key"had a different spelling. You need the same spelled key, that’s how it works. Otherwise you’re mentioning something different and it won’t be found.And as an addition to using
getExtras()– if you usegetExtras().get()and the key is not found, you will getnullin return. AlthoughDoubles can auto-box/unbox nowadays, if you doYou will still get a
NullPointerException.