2012-12-10 45 views
3

這是我目前正在做的,但它只是強制關閉應用程序。如何在Android中的活動之間傳輸布爾值?

在第一個活動

Intent myIntent = new Intent(Input.this, results.class); 
    myIntent.putExtra("perfect", rig); 
    startActivity(myIntent);` 

活動我想轉學到

Boolean lovers = getIntent().getExtras().getBoolean("perfect"); 
+0

MyModel model = new MyModel(); //1. using constructor Boolean blnObj1 = new Boolean(model.getBooleanStatus()); // This //getBooleanStatus will return 'boolean' value //2. using valueOf method of Boolean class. This is a static method. Boolean blnObj2 = Boolean.valueOf(model.getBooleanStatus()); } Intent targetIntent = new Intent(MyCalass.this, TargetClass.class); targetIntent.putExtra("STATUS", new Boolean(model.getBooleanStatus())); targetIntent.putExtra("STATUS", Boolean.valueOf(model.getBooleanStatus())); startActivity(targetIntent); 

接收端getBooleanExtra方法。但實際上在你自己的代碼中沒有錯誤。它應該完美地工作。所以你在你的問題中粘貼的代碼不會崩潰應用程序。所以你最好在應用程序崩潰後粘貼logcat數據 – stinepike

回答

4

我不知道的接受的答案 :: 但我認爲這應該是

Boolean lovers = getIntent().getExtras().getBoolean("perfect"); 
+1

你是對的。 getExtras()返回一個Bundle,Bundle沒有getBooleanExtra()方法。 – DocDevelopers

4

由於the docs說,功能getBooleanExtra

Boolean lovers = getIntent().getExtras().getBooleanExtra("perfect"); 
1

您可以在發送側試試這個:根據您可以使用@PearsonArtPhoto的答案

Intent receiverIntent = getIntent().getBoolean("STATUS"); 
相關問題