2016-03-28 68 views
0

由于某种原因,我的输入验证不起作用。每当我把它计算崩溃“应用程序”,当它应该有一个错误说我需要输入高度/重量。当我输入它计算的数字时。谢谢您的帮助 :)。我是android studio的新手。输入验证不起作用android studio

这里是我计算的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(); 
    } 

}

+0

请发布logcat –

+0

哪里有'ETtweight'属于?那是'fragment_bmi'吗? –

回答

0

试图改变类似下面的代码,

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

这工作感谢帮助我。现在如果把它放在错误的地方会更有意义。 – user6079154

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

首先,整理你的缩进和变量名了。永远不要命名变量str1,str2:始终有意义的名称。缩进应始终保持一致。这将有助于确定未来的可读性和速度。

你通过

calculateBMI() method

你的代码读取的那部分做输入验证后,你居然输入和分配的事情:让我们从文本的文本字段,解释BMI,然后看看是否文本框是空的