Android - 在两个活动之间传递数据获取NullPointerException
我有3个活动,我们将它们称为A,B和C.A和B都具有将视图发送给C的意图。设计一个C来自不同事物的设计关于C的活动。我从一个传递这样的数据,以C:Android - 在两个活动之间传递数据获取NullPointerException
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
startActivity(intent);
用C的onCreate方法
然后,我有这样的事情:
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
然后我从B到CI有
Intent intent = new Intent(this, C.class);
startActivity(intent);
然后我得到NullPointerException,我猜这是因为我从B到C时未指定字符串“Action”。
现在我可以在这种情况下为B添加一行,但是,如果有更多的活动或一个大型的项目,这不会是好事,因为对于每个活动去一个我会需要这个。
我该如何拥有它,所以我不会在没有添加“动作”刺激活动B的情况下得到此异常?
感谢您的帮助提前。
编辑
如果我有这样的从A到C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
intent.putExtra("Action2", "A");
startActivity(intent);
这从B到C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "B");
startActivity(intent);
然后在C中的onCreate失败去当从B到C:
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
else if (extras.getString("Action2").equals("A")) {
//DO Stuuf
}
}
像这样更改C类中的代码以检查包是否为空。
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getString("Action") != null)
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
}
if(extras.getString("Action2") != null)
if (extras.getString("Action2").equals("A2")) {
//DO SOMETHING
}
}
}
嘿,这工作,但如果我发送两个数据,并没有设置它的两个中断。有没有办法解决这个问题? – iqueqiorio 2014-12-04 05:55:25
发送来自A或B活动的数据并像这样以C接收它。 – 2014-12-04 06:01:22
查看编辑cahrjfakds – iqueqiorio 2014-12-04 06:02:34
检查下面的代码:
if(getIntent().hasExtra("Action"))
//if you have passed "Action" from activity
else
//if you did not pass "Action" from activity
尝试......
Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}
希望这解决了
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}
嘿,这有效,但如果我发送两个数据,并且不设置它的中断。有没有办法解决这个问题? – iqueqiorio 2014-12-04 05:58:39
NullPointerException
这不会发生,因为当你移动活动A到C你传递意图的价值,并在活动C中使用Bundle获得该值。但是,当您移动从活动B到C没有通过的意图和活动ç传递值,你写了这
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
在此TME额外为空所以你会得到错误。
DO这样
Bundle extras = getIntent().getExtras();
if(extras != null)//This one will check for null
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
编辑 根据活动
Intent intent= new Intent(this,NextActivity.class);
Bundle extra = new Bundle();
extra.putString("Action1","Value1");
extra.putString("Action2","Value2");
intent.putExtras(extra);
startActivity(intent);
在NextActivity
Intent i = getIntent();
Bundle extras = i.getExtras();
String strAction1 = extras.getString("Action1");
String strAction2 = extras.getString("Action2");
嘿,这工作,但如果我发送两个数据,并没有设置它的两个中断。有没有办法解决这个问题? – iqueqiorio 2014-12-04 05:56:36
你想从活动中传递两个值 – 2014-12-04 05:59:14
是的请参阅编辑1秒 – iqueqiorio 2014-12-04 06:00:09
你是个开始传递多个值e活动C两次(每当你调用startActivity(intent)时)。第一次来自A,第二次来自B.两次你检查这个
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
这是绑定给你一个空指针异常。在你的IDE中放置一个断点并验证它。 为什么不使用一些静态变量来共享活动之间的信息,以防万一您不想多次实例化类C。
当您移动到新的活动时,您总是创建新的Intent对象。因此这个问题。尝试以新的意图和访问权限复制所需数据。
你从B的意图没有额外的,所以当你调用extras.getString()你得到一个NPE。在下面的@Remees M Syde中检查null。 – Luis 2014-12-04 05:35:24