2016-08-04 96 views
0

任何人都可以告訴我以下聽者有什麼問題嗎?我總是崩潰...簡單的計算器不工作

@Override 
public void onClick(View view) { 
    Editable num1 = NumberOne.getText(); 
    Editable num2 = NumberTwo.getText(); 
    int um1 = Integer.parseInt(num1.toString()); 
    int um2 = Integer.parseInt(num2.toString()); 
    Results.setText(um1 + um2); 
} 
+0

你看到了哪些錯誤信息? –

+0

沒什麼,我只是在我的手機上崩潰 –

+1

發佈[mcve] .. – Reimeus

回答

2

setText方法接受一個字符串作爲參數。 um1 + um2的結果將是整數。我建議你先將結果轉換爲一個String,然後將其設置在setText方法內。

像這樣的東西應該工作:

Results.setText(Integer.toString(um1+um2)); 

更妙的是,你可以這樣做:

Results.setText(Integer.toString(Integer.parseInt(num1.toString()) + Integer.parseInt(num2.toString()))); 
1

的原因,它不工作是因爲你不能設置文本的。您必須使用Results.setText(String.valueOf(um1 + um2))(如Andre所述)將整數轉換爲字符串。因此,setText將起作用。