2017-11-21 19 views
0

BMI電卓を練習問題として書いています。何も入力せずにボタンを押すと、アプリはクラッシュし続けます。私はあなたのコードで見ることができるようにsetErrorを試しましたが、動作しません。何か案は?数字が入力されていないときにBMIアプリケーションがクラッシュする

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.text.DecimalFormat; 

public class MainActivity extends AppCompatActivity { 

    TextView textResult; 
    TextView feedbackOut; 

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

     final EditText massInputText = findViewById(R.id.massText); 
     final EditText heightInputText = findViewById(R.id.heightText); 
     textResult = findViewById(R.id.textResult); 
     final DecimalFormat df = new DecimalFormat("0.00"); 
     final Button buttonCalculator = findViewById(R.id.button); 
     feedbackOut = findViewById(R.id.feedBack); 


     buttonCalculator.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (massInputText.getText().length() == 0) { 
        massInputText.setError("Please"); 
       } 

       if (heightInputText.getText().length() == 0) { 
        heightInputText.setError("Please"); 
       } 

       double mass = Double.parseDouble(massInputText.getText().toString()); 
       double height = Double.parseDouble(heightInputText.getText().toString()); 
       double bmi = getMassData(mass, height); 
       textResult.setText(df.format(bmi).toString()); 

      } 
     }); 

    } 

    public double getMassData(double mass, double height) { 
     double square = Math.pow(height, 2); 
     double result = mass/square; 

     if (result < 18.5) { 
      feedbackOut.setText("You are underweight. You need to eat more! "); 
     } else if (result >= 18.5 & result <= 24.9) { 
      feedbackOut.setText("You are in the Healthy weight range. Good Job! "); 
     } 
     if (result >= 25 & result <= 50) { 
      feedbackOut.setText("You are Overweight. Start working out and eat more healthy! "); 
     } 

     return result; 
    } 

} 

答えて

0

私はbuttonCalculatorウィジェットに添付あなたOnClicklistenerの検証を改善することを示唆しています。

特に無効な入力の後にすばやく失敗する必要があります。エンドユーザーがエラーが発生した後にすべきことをすばやく理解できるように、意味のあるエラーメッセージを表示してください。これらの線に沿って

何か:入力が空(またはそれだけのスペースがある場合)、それは特定のフォーマットを持っている場合のどちらかであるかどうかをチェックする必要があり、この特定のケースで

buttonCalculator.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     Editable massInputStr = massInputText.getText(); 
     if (isEmpty(massInputStr) || isNotANumber(massInputStr)) { 
      massInputText.setError("Please enter the mass value (example: 12.5)"); 
      return; // Prevent further execution 
     } 

     Editable heightStr = heightInputText.getText(); 
     if (isEmpty(heightStr) || isNotANumber(heightStr)) { 
      heightInputText.setError("Please enter a valid height (example: 1.75)"); 
      return; // Prevent further execution 
     } 

     double mass = Double.parseDouble(massInputStr.toString()); 
     double height = Double.parseDouble(heightStr.toString()); 
     double bmi = getMassData(mass, height); 
     textResult.setText(df.format(bmi)); 
    } 

    private boolean isEmpty(Editable editable) { 
     return editable == null || editable.toString().trim().isEmpty(); 
    } 

    private boolean isNotANumber(Editable editable) { 
     return !editable.toString().trim().matches("\\d+(\\.\\d+)?"); 
    } 
}); 

。これは正規表現で行うことができます(isNotANumber参照)。

+0

ありがとうございます!それは素晴らしい仕事です! – AlanKor

+0

@AlanKor:あなたの問題を解決した場合は、答えを受け入れることができます:) –

関連する問題