2012-04-25 95 views
0

我想從1個活動傳遞一個字符串到另一個,雖然我從許多其他線程接受的答案中引用了,但我面臨的問題是我無法調試。當我如下面的代碼所示評論extras.putString時,Toast消息顯示正確的地址,這意味着值被正確設置並且代碼工作正常,但是當我使用extras.putString()時,由於異常,我得到NullPointerException和應用程序關閉。我的地址字符串中有很多\ n字符。逸岸就算我用extras.putString(「userAddress」,「測試」),我得到的NullPointerException通過意圖傳遞額外信息的異常

這是我從我想打電話給FBShare活動主要活動:

Intent mIntent = new Intent(this, FBShare.class); 
Bundle extras = mIntent.getExtras(); 
String currentAddress = getCurrentAddress(ourLocation); 
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show(); 
extras.putString("userAddress", currentAddress); 
startActivity(mIntent); 

而在FBShare活動我我試圖取值如下

strAddress = getIntent().getExtras().getString("userAddress"); 

Here is一個線程正在做類似的事情。

+3

看看@ akki的答案,你就錯過了'mIntent.putExtras (額外);' – thepoosh 2012-04-25 07:17:27

回答

3

盡我代碼

Intent mIntent = new Intent(this, FBShare.class); 
Bundle extras = new Bundle(); 
String currentAddress = getCurrentAddress(ourLocation); 
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show(); 
extras.putString("userAddress", currentAddress); 
mIntent.putExtras(extras); 
startActivity(mIntent); 

希望這會起作用。

3

嘗試直接把多餘的意圖:

mIntent.putExtra("Key", "Value")

此外,您檢索額外使用

Intene t = getIntent(); 
String k="key"; 
if (t.hasExtra(k)) { 
    s = t.getStringExtra(k); 
    ... 
} 

的是獲得/把許多變種類型

1
Intent mIntent = new Intent(this, FBShare.class); 
String currentAddress = getCurrentAddress(ourLocation); 
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show(); 
mIntent.putString("userAddress", currentAddress); 
startActivity(mIntent); 

包是不是需要在你的情況,因爲它似乎是你只考取您可以用上面的一串代碼..