Android Intent.getStringExtra() returns null

北城余情 提交于 2019-12-17 11:17:30

问题


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



来源:https://stackoverflow.com/questions/15555750/android-intent-getstringextra-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!