2016-05-14 125 views
-1

當我輸入數字時,該按鈕正常工作,但當我不輸入數字時它會崩潰。爲了解決這個問題,我嘗試使用條件語句,以便計算按鈕不會運行,如果沒有輸入數字,但它仍然不起作用。我放了一個其他的行,看它是否打印任何東西,但仍然沒有。Android Studio:當輸入數字時應用程序崩潰

 public void onClick(View view) { 
      //This is where I tried to fix it 
      if (percentageText.getText()!=null && numberText.getText()!=null){ 
       float percentage = Float.parseFloat(percentageText.getText().toString()); 
       float dec = percentage/100; 
       float total = dec * Float.parseFloat(numberText.getText().toString()); 
       totalTextView.setText(Float.toString(total)); 
       Log.d("myTag","Success!"); 
      } else { 
       Log.d("myTag","Error no integer found"); 
      } 
     } 

這裏的logcat的: error

但如果我解析浮動,因爲如果該語句阻止代碼被跑了吧,應該沒有關係?或者它試圖運行嗎?

+0

你有沒有試着用'試穿catch'周邊呢? –

+0

你可以在這裏發佈logcat錯誤嗎? – 2016-05-14 04:46:50

+0

你解析爲'浮動'這可能是問題。顯示您的logcat以獲取更多信息。 –

回答

0

嘗試用你的空條件檢查沿增加另一個條件檢查。

(numberText.getText().toString().trim().getlength > 0) or (! numberText.getText().toString().trim().equalignourcase("")) 

將此兩個百分比的EditText和numbertext的EditText

+0

你可以upvote答案 – DKV

0

要強制輸入數量的EditText,您可以將此屬性設置爲EditText上:

android:inputType="number" 
相關問題