Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getIntent().getStringExtra() shows null

I m using one EditText field and one spinner. I have to pass teh results of both to the next Activity. here, reqd_bloodgroup is the spinner item, i converted into String using: reqd_bloodgrp = String.valueOf(spinner.getSelectedItem()); inside onItemSelected() of spinner.

intent.putExtra("city", citySelected.getText().toString());
intent.putExtra("bloodgroup", reqd_bloodgrp);
intent = new Intent(FindDonor.this,SpecificDonor.class);
startActivity(intent);

Here when i try to display these, there's no problem. They're correctly displayed. But when i try to fetch them in SpecificDonor activity, they show null values. The code used here is:

String text_city,text_bloodgroup;
text_city = getIntent().getStringExtra("city");
text_bloodgroup = getIntent().getStringExtra("bloodgroup");
Toast.makeText(getApplicationContext(), text_city + " " + "bloodgrp: " + text_bloodgroup, Toast.LENGTH_SHORT).show();

What could be the problem?

like image 281
Chetna Avatar asked Jun 18 '12 07:06

Chetna


People also ask

Can getIntent be null?

It CAN be null when Your application was updated from the market while it was in the memory and relaunched again after the update. Maybe even If you will make update manually by Studio, or from . apk file, the same effect will be.

What is the use of getIntent ()?

you can retrieve this data using getIntent in the new activity: Intent intent = getIntent(); intent. getExtra("someKey") ... So, it's not for handling returning data from an Activity, like onActivityResult, but it's for passing data to a new Activity.


1 Answers

I think that you must do the:

intent = new Intent(FindDonor.this,SpecificDonor.class);

before adding extras. Try with:

intent = new Intent(FindDonor.this,SpecificDonor.class);
intent.putExtra("city", citySelected.getText().toString());            
intent.putExtra("bloodgroup", reqd_bloodgrp);
startActivity(intent);
like image 129
Ivan Avatar answered Nov 15 '22 09:11

Ivan