2011-11-11 32 views
1

我正在運行下面的代碼。有兩個片段。首先是我如何設置包,第二是我如何檢索它。由於某種原因,每次我檢查包時,它都返回爲空。Android意圖包總是空?

任何意見,爲什麼這是或我做錯了什麼將不勝感激。

Intent intent = new Intent(this, com.hom.app.Hom.class); 
     Bundle b = new Bundle(); 
     b.putString("WELL", "yes"); 
     intent.putExtras(b); 
    startActivity(intent); 

檢索軟件包:

String well =""; 
    Bundle bun = getIntent().getExtras(); 
    String standard = "yes"; 
    if(bun != null){  
     Log.v("Bundle", "Contains data"); 
     well = bun.getString("WELL"); 
     if(well == null) well = ""; 
     if(well == standard) method(); 
    } 
+0

你有沒有得到這個工作? – JoeLallouz

回答

1

不知道爲什麼它會返回null,但即使束通過正確的,因爲你正在做=字符串比較降臨到你的代碼是行不通的=

這條線:

 if(well == standard) method(); 

應該是

 if(well.equals(standard)) method(); 
3

當你創建你的意圖時,只需把額外的東西放在那裏。您正試圖在上面的代碼中訪問錯誤的軟件包。像這樣的東西應該工作。

Intent intent = new Intent(this, com.hom.app.Hom.class); 
    intent.putExtras("WELL", "yes"); 
    startActivity(intent); 
+0

什麼antlersoft說你的字符串比較也是如此。 – JoeLallouz

+0

我已經改變了字符串比較多數民衆贊成在。菜鳥錯誤在那裏!我也將代碼更改爲上面推薦的,但仍然返回捆綁包爲空 – Somk

+0

我已在多個位置完成此操作並且它可以正常工作。您是否找到了解決方案,或者您想要擴展您的代碼以獲得進一步的幫助? – JoeLallouz

1

documentation

的密鑰必須包括一個包前綴,例如應用com.android.contacts將使用的名稱,如 「com.android.contacts.ShowAll」。

所以不是你爲什麼不只是使用

intent.putExtra("WELL", "yes");