Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入验证无法运行android studio_Java_Android_Validation_Android Studio - Fatal编程技术网

Java 输入验证无法运行android studio

Java 输入验证无法运行android studio,java,android,validation,android-studio,Java,Android,Validation,Android Studio,由于某种原因,我的输入验证不起作用。每次我把calculate放在“app”上时,它都会崩溃,因为它应该有一个错误,说明我需要输入身高/体重。当我输入数字时,它会计算。谢谢你的帮助:)。我是安卓工作室的新手 这是我的计算java文件 public class BmiFrag extends Fragment implements View.OnClickListener { Button BmiButton; private double weight1=0; private double

由于某种原因,我的输入验证不起作用。每次我把calculate放在“app”上时,它都会崩溃,因为它应该有一个错误,说明我需要输入身高/体重。当我输入数字时,它会计算。谢谢你的帮助:)。我是安卓工作室的新手

这是我的计算java文件

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();
  }
公共类BmiFrag扩展片段实现View.OnClickListener{
按钮按钮;
私人双权重1=0;
私人双倍高度1=0;
公共静态编辑文本高度;
公共静态编辑文本权重;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图myView=充气机。充气(R.layout.fragment\u bmi,容器,false);
BmiButton=(Button)myView.findviewbyd(R.id.CalculateBmi);
bmibuton.setOnClickListener(此);
返回myView;
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.CalculateBmi:
权重=(编辑文本)
getActivity().findViewById(R.id.ETtweight);
heightIn=(EditText)getActivity().findViewById(R.id.ETHeight);
最终文本视图tv4=(文本视图)
getActivity().findViewById(R.id.TFDisplayBmi);
字符串str1=weightIn.getText().toString();
字符串str2=heightIn.getText().toString();
浮球重量=浮球。解析浮球(str1);
浮动高度=float.parseFloat(str2);
浮动BMI值=计算BMI(体重、身高);
字符串bmi解释=解释bmi(bmi值);
tv4.setText(String.valueOf(bmiValue+“-”+bmi解释));
if(TextUtils.isEmpty(str1)){
设置错误(“请输入您的体重”);
requestFocus();
返回;
}
else if(TextUtils.isEmpty(str2)){
heightIn.setError(“请输入您的身高”);
heightIn.requestFocus();
返回;
}
打破
}
}
专用浮子计算器BMI(浮子重量、浮子高度){
浮动体重指数=(浮动)(体重/(身高*身高)*4.88);
浮动总额=数学整数(bmi);
返回总数;
}
专用字符串解释BMI(浮点BMI值){
如果(BMI值<16){
返回“严重体重不足”;
}否则如果(BMI值<18.5){
返回“减持”;
}否则如果(BMI值<25){
返回“正常”;
}否则如果(BMI值<30){
返回“超重”;
}否则{
返回“肥胖”;
}
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
@凌驾
公共空间{
}
@凌驾
公共无效连接(){
super.onDetach();
}

}

尝试更改代码,如下所示:

        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));
      }

首先,对缩进和变量名进行排序。永远不要命名变量str1,str2:总是有意义的名称。压痕应始终保持一致。这将有助于提高可读性和速度,但会在将来进行修复

在您实际输入和分配内容之后,您正在进行输入验证

calculateBMI()方法


代码的这一部分是这样的:让我们从文本字段中提取文本,解释BMI,然后查看文本字段是否为空

请将日志张贴在
ETtweight
所属的位置?那是在
碎片\u bmi
里面吗?这很有效,谢谢你帮了我。现在把它放在错误的地方更有意义了。
        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));
      }