简体   繁体   中英

Input Validation not working android studio

For some reason my input Validation is not working. Every time I put calculate it crashes "app" when it should have an error saying that I need to input height/weight. When I do input the numbers it does calculate. Thanks for the help :). I'm new to android studio .

here is my calculation java file

public class BmiFrag extends Fragment implements View.OnClickListener {
Button BmiButton;
private  double weight1=0;
private  double height1=0;
public static EditText heightIn;
public static EditText weightIn;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View myView = inflater.inflate(R.layout.fragment_bmi, container, false);
   BmiButton = (Button) myView.findViewById(R.id.CalculateBmi);
    BmiButton.setOnClickListener(this);
    return myView;
}

    @Override
     public void onClick(View v) {
         switch (v.getId()) {

        case R.id.CalculateBmi:







            weightIn = (EditText)
           getActivity().findViewById(R.id.ETtweight);
            heightIn = (EditText) getActivity().findViewById(R.id.ETHeight);
               final TextView tv4 = (TextView) 
         getActivity().findViewById(R.id.TFDisplayBmi);
            String str1 = weightIn.getText().toString();
            String str2 = heightIn.getText().toString();

            float weight = Float.parseFloat(str1);
            float height = Float.parseFloat(str2) ;

            float bmiValue = calculateBMI(weight, height);

            String bmiInterpretation = interpretBMI(bmiValue);

            tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));




            if (TextUtils.isEmpty(str1)) {



                weightIn.setError("Please enter your weight");
                weightIn.requestFocus();
                return;
            }

            else if (TextUtils.isEmpty(str2)) {
                heightIn.setError("Please enter your height");
                heightIn.requestFocus();
                return;
            }



            break;






    }
    }











   private float calculateBMI(float weight, float height) {

  float bmi= (float) (weight/ (height*height)*4.88);

        float total= Math.round(bmi);



       return  total;
   }


    private String interpretBMI(float bmiValue) {

      if (bmiValue < 16) {
          return "Severely underweight";
      } else if (bmiValue < 18.5) {

        return "Underweight";
        } else if (bmiValue < 25) {

          return "Normal";
       } else if (bmiValue < 30) {

          return "Overweight";
       } else {
           return "Obese";


       }


   }


    @Override
   public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
   }

   @Override
   public void onDestroy() {


   }

  @Override
   public void onDetach() {
      super.onDetach();
  }

}

if (TextUtils.isEmpty(str1)) {



                weightIn.setError("Please enter your weight");
                weightIn.requestFocus();
                return;
            }

           if (TextUtils.isEmpty(str2)) {
                heightIn.setError("Please enter your height");
                heightIn.requestFocus();
                return;
            }

Try to change the code like the following,

        if (TextUtils.isEmpty(str1)) {
            weightIn.setError("Please enter your weight");
            weightIn.requestFocus();
            return;
        }
        else if (TextUtils.isEmpty(str2)) {
            heightIn.setError("Please enter your height");
            heightIn.requestFocus();
            return;
        }else{
        float weight = Float.parseFloat(str1);
        float height = Float.parseFloat(str2) ;

        float bmiValue = calculateBMI(weight, height);

        String bmiInterpretation = interpretBMI(bmiValue);

        tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));
      }

firstly, sort your indentation and variable names out. Never name a variable str1, str2: always meaningful names. Indentation should always be consistent. This will help out but fixing in the future for readability and speed.

You're doing input validation after you actually input and assign things through

calculateBMI() method

That part of your code reads: Lets take text from text fields, interpret the BMI and then see if the textfields are empty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM