在活动之间切换时获取NullPointer异常

时间:2013-09-13 06:20:00

标签: android android-layout android-intent android-emulator

我有3项活动

活性1 Avtivity2 Activity3

此处代码:

活性1:

Intent intent= new Intent(Activity1.this,Acivity2.class);
Bundle b=new Bundle();           
b.putParcelableArrayList("actionArray", (ArrayList<? extends Parcelable>) AllListArray.get(0)); 
b.putParcelableArrayList("comedyArray", (ArrayList<? extends Parcelable>) AllListArray.get(1));
intent.putExtras(b);             
startActivity(intent);

活性2:

//get the Bundle of movie category data data from previous activity
        Bundle recdData = getIntent().getExtras();

        actionArray=recdData.getParcelableArrayList("actionArray");
        comedyArray=recdData.getParcelableArrayList("comedyArray");


 public void btnClicker(View v)
    {
        Intent  i= new Intent(getApplicationContext(), Activity3.class);;
        Bundle b=new Bundle();
       switch(v.getId())//get the button label id from respective View
       { 
        case R.id.actionBtn:             
            b.putParcelableArrayList("movieCategorie", (ArrayList<? extends Parcelable>) actionArray); 
            b.putString("movieCategorieDisplay","Action Movies");
            i.putExtras(b);   
             startActivity(i);          
             break;

        case R.id.comedyBtn:
            b.putParcelableArrayList("movieCategorie", (ArrayList<? extends Parcelable>) comedyArray); 
              b.putString("movieCategorieDisplay","Comedy Movies");
            i.putExtras(b);   
             startActivity(i);
             break;

Activity3:

backButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent i = new Intent(getBaseContext(), Acivity2.class);
                i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
                CategoryListView.this.startActivity(i);

            }
        });

问题是当我在Activity3中单击Backbutton(在Mobile BackButton上)某些时候获取NullPionterException

活动2中此行中的异常点

actionArray=recdData.getParcelableArrayList("actionArray");

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

因为bundle变为null所以请检查你的包null,如@。

if(recdData!=null){

actionArray=recdData.getParcelableArrayList("actionArray");
 comedyArray=recdData.getParcelableArrayList("comedyArray");
}

<强>更新

甚至在调用Intent时传递适当的Context,如 Biraj Zalavadia

所提及的那样

用于:

  

Intent i = new Intent(Activity3.this,Acivity2.class);

相反:

  

Intent i = new Intent(getBaseContext(),Acivity2.class);

答案 1 :(得分:2)

在Activity3中,您使用了一个意图来启动Activity2。你没有添加任何额外内容。因此,当您在Activity2中获取捆绑包时

Bundle recdData = getIntent().getExtras();

实际上是空的。您应该在使用之前进行检查,如下所示:

Bundle recdData = getIntent().getExtras();
if(recdData != null) {
    actionArray=recdData.getParcelableArrayList("actionArray");
    comedyArray=recdData.getParcelableArrayList("comedyArray");
}

您还应该知道,使用当前导航,当您从Activity3启动Activity2时,您将无法获得actionArraycomedyArray

答案 2 :(得分:2)

试试这个

你得到的是getBaseContext()null

backButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent i = new Intent(Activity3.this, Acivity2.class); //UPDATE HERE
                i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
                CategoryListView.this.startActivity(i);

            }
        });