2017-04-24 30 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 
    }