0

我有3個活動,我們將它們稱爲A,B和C.A和B都具有將視圖發送給C的意圖。設計一個C來自不同事物的設計關於C的活動。我從一個傳遞這樣的數據,以C:Android - 在兩個活動之間傳遞數據獲取NullPointerException

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的情況下得到此異常?

感謝您的幫助提前。

編輯

如果我有這樣的從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 
    } 
} 
+1

你從B的意圖沒有額外的,所以當你調用extras.getString()你得到一個NPE。在下面的@Remees M Syde中檢查null。 – Luis 2014-12-04 05:35:24

回答

2

像這樣更改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 
     } 
    } 
} 
+0

嘿,這工作,但如果我發送兩個數據,並沒有設置它的兩個中斷。有沒有辦法解決這個問題? – iqueqiorio 2014-12-04 05:55:25

+0

發送來自A或B活動的數據並像這樣以C接收它。 – 2014-12-04 06:01:22

+0

查看編輯cahrjfakds – iqueqiorio 2014-12-04 06:02:34

0

檢查下面的代碼:

if(getIntent().hasExtra("Action")) 
    //if you have passed "Action" from activity 
else 
    //if you did not pass "Action" from activity 
0

嘗試......

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

希望這解決了

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

嘿,這有效,但如果我發送兩個數據,並且不設置它的中斷。有沒有辦法解決這個問題? – iqueqiorio 2014-12-04 05:58:39

0

NullPointerException這不會發生,因爲當你移動活動A到C你傳遞意圖的價值,並在活動C中使用Bundle獲得該值。但是,當您移動從活動B到C沒有通過的意圖和活動ç傳遞值,你寫了這

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

在此TME額外爲空所以你會得到錯誤。

DO這樣

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"); 
+0

嘿,這工作,但如果我發送兩個數據,並沒有設置它的兩個中斷。有沒有辦法解決這個問題? – iqueqiorio 2014-12-04 05:56:36

+0

你想從活動中傳遞兩個值 – 2014-12-04 05:59:14

+0

是的請參閱編輯1秒 – iqueqiorio 2014-12-04 06:00:09

0

你是個開始傳遞多個值e活動C兩次(每當你調用startActivity(intent)時)。第一次來自A,第二次來自B.兩次你檢查這個

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

這是綁定給你一個空指針異常。在你的IDE中放置一個斷點並驗證它。 爲什麼不使用一些靜態變量來共享活動之間的信息,以防萬一您不想多次實例化類C。

0

當您移動到新的活動時,您總是創建新的Intent對象。因此這個問題。嘗試以新的意圖和訪問權限複製所需數據。

相關問題