2016-01-20 59 views
0

爲什麼該值沒有出現在第二個活動中,那就是consultDoctorAnaemia?我認爲代碼已經是正確的了。但它顯示爲空。將意圖值傳遞給另一個活動

resultAnaemia

if(symptom16.equals("Yes")) 
{         
    weight=0.11;             
    newWeight = 0.0 * 0.15; //cf disease = 0.6, [min=0.15]        
    String cf = Double.toString(newWeight); 


    Intent intent = new Intent(resultAnemia.this, consultDoctorAnaemia.class); 

    intent.putExtra("cfDiseases", cf); 

    startActivity(intent); 

} 

consultDoctorAnaemia

TextView textView = (TextView) findViewById(R.id.textCF); 

    //get passed intent 
    Intent intent = getIntent(); 

    if(null != intent) 
    { 
     //get cf value from intent 
     String cf = intent.getStringExtra("cfDiseases"); 
     textView.setText("Certainty value : " + cf); 
    } 
+0

以什麼方法你執行你'consultDoctorAnaemia'發佈的代碼?另外,發佈你的清單並解釋活動之間的導航(例如:當你調用'startActivity()'啓動'consultDoctorAnaemia'時,哪些活動已經活動。 –

回答

0

您需要在consultDoctorAnaemia活動要做到:

Bundle bundle = getIntent().getExtras(); 

String value2 = bundle.getString("cfDiseases"); 
+0

胡說八道!這段代碼與OP的發佈完全一樣**代碼! –

+0

nop,OP正在獲取意圖,但不是額外的...... –

+0

調用'Intent'上的'getStringExtra()'與您發佈的內容完全相同**,請親自嘗試。 –

1
Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      String Diseases = extras.getString("cfDiseases"); 
     } 
0

在第一個活動試試這個:

// do your intent setup somewhere and then setup bundle 
Bundle info = new Bundle(); 
info.putString("cfDiseases", cf); 
intent.putExtras(info); 
startActivity(intent); 

In new activity: Bundle info = new Bundle(); info = getIntent()。getExtras(); cf = info.getString(「cfDiseases」);

0

您也可以通過這樣的方式

值聲明你的字符串全局和靜態例如

變量聲明在這個類

A類

public class A extends Activity{ 

static String cf = "abcde"; 


} 

訪問變量在這個B類

B類

public class B extends Activity{ 
String Temp; 

//you can get variable like this 
Temp=A.cf ; 
Toast.makeText(B.this, "Temp = "+Temp, Toast.LENGTH_SHORT).show(); 

} 
相關問題