2013-09-27 38 views
0

我試圖從一個活動傳遞兩個數字輸入到其他的UI,但是當我點擊按鈕來改變意圖崩潰。如何將意圖數據設置爲當前活動editText?

我在logcat中得到了以下內容:http://pastebin.com/zkWPcSNZ,這表明我在解析數據到CalcResult中的editTexts時出現了問題。

我的問題是,什麼是錯的,我試圖將數據傳遞到CalcResult editText.Is有acheiving本的替代方法的方式嗎?

我的兩個類看起來像這樣以供參考:

public class MainActivity extends Activity implements OnClickListener { 

    //variables for xml objects 
    EditText offsetLength,offsetDepth,ductDepth; 
    Button calculate; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //setting the variables to the xml id's and setting the click listener on the calc button 
     offsetLength = (EditText)findViewById(R.id.offLength); 
     offsetDepth = (EditText)findViewById(R.id.offDepth); 
     ductDepth = (EditText)findViewById(R.id.ductDepth); 
     calculate = (Button)findViewById(R.id.calc); 
     calculate.setOnClickListener(this);//don't cast the listener to OnClickListener 


    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     try { 

      String getoffsetlength = offsetLength.getText().toString(); 
      String getoffsetdepth = offsetDepth.getText().toString(); 
      String getductdepth = ductDepth.getText().toString(); 

      double tri1,tri2; 
      double marking1,marking2; 

      double off1 = Double.parseDouble(getoffsetlength); 
      double off2 = Double.parseDouble(getoffsetdepth); 
      double off3 = Double.parseDouble(getductdepth) 
        ; 
      marking1 = Math.pow(off1,2) + Math.pow(off2,2); 
      tri1 = (float)off2/(float)off1; 
      tri2 = (float)off3/Math.atan((float)tri1); 
      marking2 = (float)off3/Math.atan(tri2); 


      Intent myIntent = new Intent(MainActivity.this, CalcResult.class); 
      myIntent.putExtra("numbers", marking1); 
      myIntent.putExtra("numbers", marking2); 

      startActivity(myIntent); 


     } catch (NumberFormatException e) { 
      // TODO: handle exception 
      System.out.println("Must enter a numeric value!"); 

     } 

    } 


} 

這是我在傳遞數據的活動:

public class CalcResult extends MainActivity 
{ 
    EditText result1,result2; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
     result1 = (EditText)findViewById(R.id.mark1); 
     result2 = (EditText)findViewById(R.id.mark2); 

     Intent intent = getIntent(); 
     Bundle bundle = intent.getExtras(); 
     double mark1 = bundle.getDouble("number1"); 
     double mark2 = bundle.getDouble("number2"); 

     int a = Integer.valueOf(result1.getText().toString()); 
     int b = Integer.valueOf(result2.getText().toString()); 

     result1.setText(a + " "); 
     result2.setText(b + " "); 

    } 

} 

回答

0

錯誤在於你正在使用相同的密鑰「numbers」,並試圖通過另一個鍵「數字1「和」number2「。你的代碼應該是這樣的:

Intent myIntent = new Intent(MainActivity.this, CalcResult.class); 
myIntent.putExtra("number1", marking1); 
myIntent.putExtra("number2", marking2); 

和中檢索他們,你應該使用:

Intent intent = getIntent(); 
double mark1 = intent.getDoubleExtra("number1", 0); 
double mark2 = intent.getDoubleExtra("number2", 0); 

,然後設置你的EditTexts變量是這樣的:

result1 = (EditText)findViewById(R.id.mark1); 
result2 = (EditText)findViewById(R.id.mark2); 
result1.setText(mark1+""); 
result2.setText(mark2+""); 
+0

這是由OP產生的錯誤,但不是應用程序崩潰的原因。 'getDouble(String鍵)'_「返回與給定鍵關聯的值,或0.0,如果沒有所需類型的映射存在給定鍵」如果你看看他在其中發送額外的變量的代碼_ –

+0

中,他使用的關鍵是相同的'myIntent.putExtra(「數字」,marking1);',所以關鍵是「數字」,而他正試圖中檢索鍵「數字1」「和數字2」的雙重額外的,所以它將返回0。因此,即使假設他已將文本設置爲EditText,它也不會顯示任何內容 – Houcine

+1

它將返回0,但這不會引發異常。你只需要'mark1 = 0;'和'mark2 = 0;' –

1

當你開始你的第二個活動,無論是EDITTEXT空

所以,當你這樣做:

int a = Integer.valueOf(result1.getText().toString()); 
int b = Integer.valueOf(result2.getText().toString()); 

它等同於:

int a = Integer.valueOf(""); 
int b = Integer.valueOf(""); 

會拋出異常

Caused by: java.lang.NumberFormatException: Invalid int: "" 

如果你想設置他們,你通過這兩個活動傳遞的值,你可以這樣做:

double mark1 = bundle.getDouble("number1"); 
double mark2 = bundle.getDouble("number2"); 

result1.setText(mark1 + " "); 
result2.setText(mark2 + " "); 
+0

當我使用此代碼我只是得到了計算results.I 0.0和0.0認爲它可能是與數據正在越過方式有問題。 – user2815899

+0

現在你可以檢查@Houcine陳述。你的意圖是傳遞相同的密鑰。 –

+0

雅謝謝我檢查了@Hucine修復,它工作。 :)你有任何活動之間滑動的代碼示例? – user2815899

0

你發送意圖時使用相同的名字作爲您的演員,請嘗試更正。

相關問題