Android的 - 两个活动之间传递数据得到的NullPointerException [英] Android - Pass Data Between Two Activities get NullPointerException

查看:110
本文介绍了Android的 - 两个活动之间传递数据得到的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个活动,我们将称他们为A,B和C. A和B都具有发送视图C.上一架C来自不同的事情发生基于C的活动意图和设计。我通过这样的数据从A到C:

 意向意图=新的意图(这一点,C.class);
    intent.putExtra(行动,A);
    startActivity(意向);
 

则在C的onCreate方法,我有这样的事情:

 捆绑额外= getIntent()getExtras()。
如果(extras.getString(行动)。等于(A)){
//做一点事
}
 

然后,我从B到C我有

 意向意图=新的意图(这一点,C.class);
 startActivity(意向);
 

然后,我得到了NullPointerException异常,我猜这是因为我还没有specifed一个字符串行动,从B到C下。

现在我只需要添加一行B在此情况下,但是,如果有更多的活动,或一个大的项目,这将不会是很好的,因为对于每个活动去一个我需要这个。

如何才能拥有它,所以我不无添加行动蜇到b活动得到这个例外?

感谢您提前帮助。

修改

如果我有这样的从A到C

 意向意图=新的意图(这一点,C.class);
    intent.putExtra(行动,A);
    intent.putExtra(措施2,A);
    startActivity(意向);
 

这从B到C

 意向意图=新的意图(这一点,C.class);
    intent.putExtra(行动,B);
    startActivity(意向);
 

和那么这在C的onCreate从B到C下失败:

 捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
    如果(extras.getString(行动)。等于(A)){
    //做一点事
    }
    否则,如果(extras.getString(措施2)。等于(A)){
     // DO Stuuf
    }
}
 

解决方案

更​​改code在C级这样的检查束为空或不是。<​​/ P>

 捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
   如果(extras.getString(行动)!= NULL)
      如果(extras.getString(行动)。等于(A)){
        //做一点事
      }
   }
 如果(extras.getString(措施2)!= NULL)
      如果(extras.getString(措施2)。等于(A2)){
        //做一点事
      }
   }
}
 

I have 3 activities we will call them A, B and C. A and B both have intents that send the view to C. And design on which one C came from different things happen on C's activity. I pass the data like this from A to C:

Intent intent = new Intent(this, C.class);
    intent.putExtra("Action", "A");
    startActivity(intent);

Then in C's onCreate method I have something like this:

Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}

Then I from B to C I have

 Intent intent = new Intent(this, C.class);
 startActivity(intent);

And then I get the NullPointerException, I am guessing this is because i have not specifed a String "Action" when going from B to C.

Now I could just add one line for B in this case but, if there were for more activities or a large project this would not be good, because for each activity going to one I would need this.

How can I have it so I don't get this exception without adding an "Action" sting to activity B?

Thanks for the help in advance.

EDIT

If I have this from A to C

Intent intent = new Intent(this, C.class);
    intent.putExtra("Action", "A");
    intent.putExtra("Action2", "A");
    startActivity(intent);

And this from B to C

 Intent intent = new Intent(this, C.class);
    intent.putExtra("Action", "B");
    startActivity(intent);

And then this in onCreate of C it fails when going from B to 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
    }
}

解决方案

Change the code in C class like this for checking the bundle is null or not.

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
      }
   }  
}

这篇关于Android的 - 两个活动之间传递数据得到的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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