intent.getStringExtra() 返回 null [英] intent.getStringExtra() returning null

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

问题描述

我真的觉得这很奇怪,我可以获得除一个以外的其他值.这是我的代码:

I really find this weird, I'm able to get other values except one. Here's my code:

Intent intent = new Intent(this.context, SecondActivity.class);
intent.putExtra("contact", n.getContact());
intent.putExtra("email", n.getEmail());
intent.putExtra("address", n.getAddress());
intent.putExtra("test", "hello world");
context.startActivity(intent);

这是我将在 OnCreate() 下获取值的代码:

And this is the code where I'll get the values under OnCreate():

Intent intent = getIntent();
contact = intent.getStringExtra("contact");
email = intent.getStringExtra("email");
address = intent.getStringExtra("address");
test = intent.getStringExtra("test");

除了字符串 test 之外,一切都运行良好.它总是会给我空值.有什么解决办法吗?

Everything works well except for the String test. It will always give me null value. Any solution for this?

问题补充:intent.putExtra() 在第二个参数上不再接受带引号的文本吗?哈哈

Question added: Does intent.putExtra() doesn't accept quoted text anymore on the second parameter? LOL

推荐答案

在 MainActivity.class

In MainActivity.class

Intent myIntent=new Intent(MainActivity.this, ResultActivity.class);
Bundle bundle=new Bundle();
bundle.putString("contact", n.getContact());
bundle.putString("email", n.getEmail());
bundle.putString("address", n.getAddress());
bundle.putString("test", "hello world");
myIntent.putExtra("MyPackage", bundle);
startActivity(myIntent);

在 ResultActivity.class 中

In ResultActivity.class

Intent callerIntent=getIntent();
Bundle packageFromCaller=
callerIntent.getBundleExtra("MyPackage");
String contact =packageFromCaller.getString("contact");
String email =packageFromCaller.getString("email");
String address= packageFromCaller.getString("address");
String test= packageFromCaller.getString("test");

希望.它会帮助你!!!

Hope. It will help you !!!

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

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