Android Intent.getStringExtra() 返回 null [英] Android Intent.getStringExtra() returns null

查看:81
本文介绍了Android Intent.getStringExtra() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将字符串添加到 Extras 的方式:

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();

这是我尝试在 onActivityResult() 中提取它们的方式:

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);

但是在第二个代码块之后 namedescnull 的,尽管 prior 是正确的价值.此外,在调试器中,我可以看到 data.mExtras.mMap 包含所需的字符串,但仅在第一次请求之后.

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.

推荐答案

当您插入 Extras 时尝试添加 .toString()

When you insert your Extras trying adding .toString()

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

您在其中看到了 CharSequence 值,但您需要将其转换为 String 以调用 getStringExtra().显然,只需对 Strings 执行此操作.您会看到 int 的正确值,因为这是正确完成的

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

这篇关于Android Intent.getStringExtra() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆