2017-04-24 80 views
0

考虑应用程序有三个活动即A1,A2,A3:明确意图的错误,而与putExtra()工作

A1与它附加的价值要求A2的意图 “值”

Intent i=new Intent(A1.this,A2.class); 
i.putExtra("value",editTextVal); //editTextVal is got from an editText during Runtime 

哪里A2接受来自A1,并将其存储“样品”在所附值:

sample=getIntent().getExtra().getString("value"); 

现在控制前进到A3从A2 .IE,A2意图至A3,现在当A3调用活动A2有发生电子RROR因为A2具有它试图从那里作为A3使用的意图没有.putExtra()它只是这个意图获得附加数据的.getExtra()

Intent i3=new Intent(A3.this,A2.class); 

所以有发生运行时错误..请帮助我解决这个..

+2

其它一些其他的价值把你的堆栈跟踪这里 –

+0

使用它像捆B = getIntent()getExtra()。 if(b!= null){} –

回答

0

当您从A3移动到A2,为一个Bundle对象,但因为你没有从A3传递任何值A2活动搜索到A2它给零点异常 的一种方式,你可以阻止这种设置一个标志(静态变量),并且当你从A1转换到A2时,给它指定任意值1如果FLAG == 1尝试获取Bundle对象,那么您的A2活动检查FLAG的值。每当你从A2移动一定要改变标志设置为大于1

 //Declare a Variable FLAG in A1 as 
     public static int FLAG; 

     // For transition from A1 to A2 
     Intent I =new Intent(A1.this,A2.class); 
     I.putExtras("Key","Value"); 
     FLAG=1; 
     startActivity(I); 

     //on your A2 activity 
     if(A1.FLAG==1) 
     { 
      Bundle extras=getIntent().getExtras(); 
      String Value=extras.getString("Key"); 
     } 

     //When you make a transition to A3 

     Intent i1=new Intent(A1.this,A2.class); 
     A.FLAG=2; 
     startAcitivy(i1); 
0
if(getIntent().hasExtra("value")) { 
     sample=getIntent().getStringExtra("value"); 
    } 

    /* while using sample check for null 

    */ 
    if(!TextUtils.isEmpty(sample)) { 
     // use sample here 
    }