Android Intent.getStringExtra() returns null

2019-01-23 10:58发布

问题:

This is how strings are being added to Extras:

Intent i = new Intent();
i.putExtra("Name", edt_name.getText());
i.putExtra("Description", edt_desc.getText());
i.putExtra("Priority", skb_prior.getProgress());
setResult(RESULT_OK, i);
finish();

This is how I try to extract them in onActivityResult():

String name = data.getStringExtra("Name");
String desc = data.getStringExtra("Description");
int prior   = data.getIntExtra("Priority", 50);

But after the second code block name and desc are null's, though prior has it's proper value. Moreover, in debugger I can see, that data.mExtras.mMap contains needed Strings, but only after first request to it.

回答1:

When you insert your Extras trying adding .toString()

i.putExtra("Name", edt_name.getText().toString());

You are seeing the CharSequence value in there but you need to convert it to a String to call getStringExtra(). Obviously, just do this for the Strings. You see the correct value for your int because that is done correctly