getExtras(name).getString和getIntent().getStringExtra(name)之间的区别 [英] Difference between getExtras(name).getString and getIntent().getStringExtra(name)

查看:363
本文介绍了getExtras(name).getString和getIntent().getStringExtra(name)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序上有一些错误报告,它是Activity中onCreate()中的Nullpointerexception. 失败的代码是getIntent().getExtras().getStringExtra("name").(Nullpointerexception)

I have some error reports on my Android app, it's a Nullpointerexception in onCreate() in an Activity. The code that fails is getIntent().getExtras().getStringExtra("name"). (Nullpointerexception)

这意味着getExtras()以某种方式为null.我确信我会在创建意图的每个位置上额外设置意图.我无法在设备上的仿真器上重新创建它.我认为这是在我尝试再次打开应用程序后在我的真实设备上发生的(但不是在调试时发生),与此同时,Android可能终止了该进程并重新创建了活动.但是,即使在这种情况下,也不应该保留意图的附加功能吗?

That means that getExtras() is null somehow. I am sure that I am setting the intent extra on every place I am creating the intent. I can't recreate it on my emulator on device. I think it happened on my real device (but not while I was debugging) after I tried to open the app again, in the mean time Android probably killed the process and recreated the activity again. But shouldn't be the intent extras kept even in this scenario?

我试图杀死模拟器上的进程,再次调用onCreate,并且getExtras()返回正确的值.

I tried to kill the process on the emulator, onCreate was called again and the getExtras() returned the right value.

我用getIntent().getStringExtra()替换了代码.除了将不会引发nullpointerexception而是将String设置为null之外,还有什么区别.还有其他区别吗?

I replaced the code with getIntent().getStringExtra(). What's the difference besides it won't throw a nullpointerexception but will still set the String as null. Is there any other difference?

可能是什么原因造成的?

What could be causing it?

推荐答案

我发现在代码的其他地方,我正在创建带有String []的快捷方式. Android HOME(可能还有系统的其他部分)不允许使用字符串数组,而仅允许使用基本附加功能(int,string,long,float ...). 但是导致问题的代码并没有使用快捷方式,这只是一个简单的活动,它额外接收了一个字符串数组.也许空的附加组件也是由此问题引起的-应用程序被OS杀死,并且附加组件无法存储. 在这种情况下,Android不会抛出异常.

I found out that somewhere else in my code I was creating shortcut intents which had a String[] extra. Android HOME (and maybe other parts of the system) don't persit string arrays, only primitive extras (int, string, long, float...). But the code that was causing problems doesn't use shortcuts, it's only a simple activity which receives a string array extra. Maybe the empty extras were caused by this problem too - the application was killed by OS and the extras could not be stored. No exception is thrown by Android in this case.

在这里由罗曼·盖伊(Romain Guy)描述: http://groups.google.com/group/android-开发人员/browse_thread/thread/7f2ce458bd5d112f/189e2b7b2b2532d7

Described by Romain Guy here: http://groups.google.com/group/android-developers/browse_thread/thread/7f2ce458bd5d112f/189e2b7b2b2532d7

我在这里问了一个更具体的问题: 重新启动后丢失了快捷方式的额外内容?

I asked a more specific question about this here: Shortcut intent extras lost after restart?

这篇关于getExtras(name).getString和getIntent().getStringExtra(name)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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