intent.getStringExtra() 返回 null

编程入门 行业动态 更新时间:2024-10-20 20:53:46
本文介绍了intent.getStringExtra() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 02:58:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034402.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:intent   getStringExtra   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!