![](/img/trans.png)
[英]pass data between classes (not activities) results in nullpointerexception
[英]Android - Pass Data Between Two Activities get NullPointerException
我有3个活动,我们将它们称为A,B和C。A和B都具有将视图发送给C的意图。并且设计一个C来自C的活动中来自不同事物的C。 我将这样的数据从A传递到C:
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添加“动作” Action的情况下获得此异常?
我在这里先向您的帮助表示感谢。
编辑
如果我有这个从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
}
}
}
检查以下代码:
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
}
NullPointerException
之所以会发生,是因为当您从活动A转移到C时,您传递的是带有值的意图,而在活动C中,您是使用Bundle获取该值的。 但是,当您从活动B移到C时,您并没有通过意图传递值,因此在活动C中您已经编写了此
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
在这个时间,多余的为空,所以您将得到错误。
像这样
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");
您将两次启动活动C(无论何时调用startActivity(intent))。 第一次来自A,第二次来自B。这两次您都在检查
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
这势必会给您一个空指针异常。 在您的IDE中设置一个断点并进行验证。 如果不想多次实例化类C,为什么不使用一些静态变量在活动之间共享信息。
当您移至新活动时,您总是在创建新的Intent对象。 因此,这个问题。 尝试以新的意图和访问权限复制所需的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.