2011-06-21 58 views
2

我的兩項活動之間傳遞的價值觀和獲取的值是這樣的:Android的意圖getExtras

Bundle extras = getIntent().getExtras(); 
    if (extras != null) 

    { 
     initialUrl = extras.getString("initialUrl"); 
     isFollow = extras.getString("isFollow"); 
    } 


    if (isFollow == "true") { 
     editUrl.setText(initialUrl); 
     setUpWebView(initialUrl); 
    } else if (isFollow == "false") { 
     editUrl.setText("http://www.google.com"); 
     setUpWebView("http://www.google.com"); 
    } 

問題我可以通過添加手錶變量看到在調試窗口中檢索的值,但是當編譯器輸入語句if(isFollow ==「true」),條件失敗。其他情況也沒有處理。我還需要做些什麼來確保我的條件得到滿足?

+0

首先發送捆綁布爾值,如果你發送文本,然後比較使用等於不使用== – ingsaurabh

+0

是的,你的權利,saurabh。 – Hitendra

+0

==測試引用相等。 .equals()測試值是否相等。 http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Nepster

回答

2

您應該使用

isFollow.equals("true") 
在聲明

1

如果數據的字符串類型放在包,然後用下面的代碼

String isFollow = null;  
Bundle extras = getIntent().getExtras(); 
    if (extras != null) 

    { 
     initialUrl = extras.getString("initialUrl"); 
     isFollow = extras.getString("isFollow"); 
    } 
if (isFollow.equals("true")) { 
     editUrl.setText(initialUrl); 
     setUpWebView(initialUrl); 
    } else if (isFollow.equals("false")) { 
     editUrl.setText("http://www.google.com"); 
     setUpWebView("http://www.google.com"); 
    } 

嘗試,如果布爾類型的數據放在包,然後用下面的代碼

boolean isFollow = null;  
    Bundle extras = getIntent().getExtras(); 
     if (extras != null) 

     { 
      initialUrl = extras.getString("initialUrl"); 
      isFollow = extras.getBoolean("isFollow"); 
     } 
    if (isFollow) { 
      editUrl.setText(initialUrl); 
      setUpWebView(initialUrl); 
     } else { 
      editUrl.setText("http://www.google.com"); 
      setUpWebView("http://www.google.com"); 
     } 
0

你需要嘗試測試 isFollow.equals("true")或者如果它是布爾值而不是字符串,或者是 isFollow == true或者只是普通isFollow

(注意,第二個無引號)

0

我知道這是一個非常晚的答案,但它會更好地做到這一點,如果你傳遞一個布爾到意圖從發件人:

Boolean isFollow = extras.getBoolean("isFollow"); 

if(isFollow) { 
    //Do stuff 
}