2011-05-10 110 views
28

我想在兩個活動之間傳遞一個字符串。我在其他項目中使用相同的方法完成了這項工作,但出於某種原因,當我調用intent.getStringExtra(String)時,我得到一個NullPointerException異常。我也嘗試通過Android getIntent()。getExtras()返回null

Bundle b = getIntent().getExtras(); 

創建一個套件,但也返回null。以下是我目前正在嘗試使用的代碼。

活動答:

Intent myIntent = null; 
    String select = ""; 
      if (selection.equals("Chandelle")) { 
       myIntent = new Intent(Commercial.this, Chandelle.class); 
       select = "Chandelle"; 
      } else if (selection.equals("Eights on Pylons")) { 
       myIntent = new Intent(Commercial.this, EightsOnPylons.class); 
       select = "Eights on Pylons"; 
      } 
// Start the activity 
    if (myIntent != null) { 
     myIntent.putExtra("selection", select); 
     Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection")); 
     startActivity(myIntent); 
    } 

下面是b活動,試圖拉額外的代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Intent i = getIntent(); 

    if (i == null) 
     Log.d("***DEBUG****", "Intent was null"); 
    else 
     Log.d("**** DEBUG ***", "Intent OK"); 

    String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line 
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID); 

我已經試過幾乎路過羣衆演員的每一個替代方式,但他們都似乎表現得這樣。我錯過了什麼?

+0

你是如何解決這個問題的? – aProgrammer 2012-05-08 10:07:34

+0

另一個解決方案:getExtras()。get(「sharedString」) – fungusanthrax 2016-08-26 23:16:30

回答

1

看看你的代碼,我認爲當你在Activity 1中放置任何東西時可能會發生。因爲你使用了「if ... else if」,並且你沒有其他情況下的「else」。如果發生這種情況,您將在活動2中獲得空值。再次檢查此情況。

+1

沒有運氣。我應該更具體地介紹我發佈的活動B實際上是EightsOnPylons.java。另外,日誌。在活動中打印「OUTBOUND INTENT」的d語句確實顯示了正確的字符串,所以我非常肯定額外的內容實際上是以意圖發送的。 – FlapsFail 2011-05-10 02:49:39

+8

發現問題。這是程序設計的問題,而不是代碼。活動B是一個叫做活動C的tabhost。我試圖從活動C中拿出額外的東西,但是額外的東西只能從A發送到C. – FlapsFail 2011-05-10 03:03:20

7

幸運的是,我發現這個職位。我一直在掙扎幾個小時,最終我意識到我也在向tabHost發送我的意圖。 ;-)對於那些仍然存在問題的人,只需從tabHost活動本身獲取額外內容,並將其傳遞給子選項卡。

String userID; 

。 。

Bundle getuserID = getIntent().getExtras(); 
    if (getuserID != null) { 
     userID = getuserID.getString("userID"); 
    } 

...

intent = new Intent().setClass(this, Games.class); 
    intent.putExtra("userID", userID); 
    spec = tabHost.newTabSpec("games").setIndicator("Games", 
         res.getDrawable(R.drawable.tab_games)) 
        .setContent(intent); 
    tabHost.addTab(spec); 
11

添加.ToString()myIntent.putExtra("selection", select);使其myIntent.putExtra("selection", select.ToString());

+3

是的,我加了String.valueOf,它起作用! – Cheung 2013-09-12 08:26:49

+0

這也解決了我的問題,因爲我傳遞了EditText.getText()這是一個Editable對象,即使putExtra()接受兩個字符串,編譯器也不會抱怨將它傳遞給一個Editable對象。 – glenneroo 2014-02-20 21:07:48

+1

您需要爲EditText.getText()返回的Editable對象放置String.valueOf()的原因是它將direclt映射到Serializable,因此使用putExtra(String,Serializable),因此getSerializableExtra(String)需要叫做。 – TheIT 2014-08-05 20:45:51

2

如果您還沒有放任何東西捆綁,它總是返回null

您可以創建並通過自己設置的包:

Intent i = new Intent(...); 

i.putExtras(new Bundle()); 

Bundle bundle= i.getExtras(); // (not null anymore) 

或者,把虛值,只是爲了迫使初始化(我更喜歡第一個解決方案):

Intent i = new Intent(...); 

i.putExtra("dummy", true); 

Bundle bundle= i.getExtras(); // (not null anymore) 
-1

在活動B你在以下功能:

protected String getStringExtra(Bundle savedInstanceState, String id) { 
    String l; 
    l = (savedInstanceState == null) ? null : (String) savedInstanceState 
      .getSerializable(id); 
    if (l == null) { 
     Bundle extras = getIntent().getExtras(); 
     l = extras != null ? extras.getString(id) : null; 
    } 
    return l; 
} 

你正在以下面的方式使用它在交流Tivity B:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    String MANEUVER_ID = getStringExtra(savedInstanceState, "selection");  
} 
4

請試試下面的代碼。我已經爲你的代碼添加了一個覆蓋,這將做到這一點:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Intent i = getIntent(); 

    if (i == null) 
     Log.d("***DEBUG****", "Intent was null"); 
    else 
     Log.d("**** DEBUG ***", "Intent OK"); 

    String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line 
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID); 
}