2016-03-28 28 views
0

Bazı nedenlerden dolayı Giriş Doğrulama çalışmıyor. Ben her zaman koymak o zaman ben yükseklik/kilo girmeniz gerektiğini söyleyerek bir hata olması gerektiğini "app" çöküyor. Sayıları girdiğimde hesaplar. Yardım için teşekkürler :). Ben android stüdyosunda yeniyim. Burada Giriş Doğrulama çalışmıyor android studio

öncelikle dışarı sıralamak girinti ve değişken adları benim hesaplama java dosya

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

} aşağıdaki gibi kodunu değiştirmek için

+0

logcat –

+0

lütfen posta gönderin »ETtweight' nereye aittir? Bu fragment_bmi' içinde mi? –

cevap

0

deneyin,

 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

Bu bana yardım ettiğin için teşekkürler. şimdi yanlış yerde daha mantıklı. – 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

olduğunu . Hiçbir zaman str1, str2 değişkenini belirtin: her zaman anlamlı isimler. Girinti her zaman tutarlı olmalıdır. Bu, gelecekte okunabilirlik ve hız için yardımcı olacaktır.

Sen

calculateBMI() method

Kodunuzdaki okur bu kısmı

aracılığıyla şeyler aslında girişinden sonra giriş doğrulaması yapıyor ve atamak ediyoruz: textfields olmadığını görmek ardından, metin alanlarına metin almak BMI yorumlamak ve Lets boş

İlgili konular