2016-03-28 82 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,然後看看是否文本框是空的