Intent的多余部分始终为null [英] Intent's extra is always null
本文介绍了Intent的多余部分始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将int
变量传递给另一个活动:
I'm trying to pass an int
variable to another activity:
当前活动中:
Intent intent = new Intent(getApplicationContext(),PlayActivity.class);
intent.putExtra("position", position);
startActivity(intent);
在PlayActivity.onCreate
:
Intent intent = getIntent();
String position = intent.getStringExtra("position");
int index = Integer.parseInt(position);
问题是,位置始终为null
(并且parseInt()
引发异常).
为什么?
Problem is, position is always null
(and parseInt()
throws exception).
Why?
推荐答案
由于它是int
,因此没有多余的字符串.你应该有
There is no string extra to get since it is an int
. You should have
Intent intent = getIntent();
int position = intent.getIntExtra("position");
我不确定您为什么要尝试获取String
然后将其作为int
发送时解析为int
,假设position
在您的第一个Activity
中是int
.如果不是这种情况,请给我更好的解释.
I'm not sure why you are trying to get a String
then parse to an int
when it is sent as an int
, assuming position
is an int
in your first Activity
. If that is not the case then please explain a little better.
这篇关于Intent的多余部分始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文