-3
votes

Ma condition ne fonctionne pas mon application se bloque comme bouton clic

J'essaie de vérifier si l'editext est vide ou non. La variable pour EditText est dans le type de flotteur.

07-31 12:04:16.363 26780-26780/com.example.iubmeritcalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iubmeritcalculator, PID: 26780
java.lang.NumberFormatException: Invalid float: ""at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseFloat(StringToReal.java:308) at java.lang.Float.parseFloat(Float.java:306) at java.lang.Float.valueOf(Float.java:343) at android.view.View.performClick(View.java:5052)at android.view.View$PerformClick.run(View.java:20162) at android.os.Handler.handleCallback(Handler.java:739)at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5753) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:145) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)


1 commentaires

java.lang.numberformatException: double invalide: "" n'est pas ce message clair? Si le champ est vide, vous obtenez une chaîne vide qui n'est pas un Souble


3 Réponses :


2
votes

Vous essayez d'analyser une chaîne vide dans un flotteur, ce qui échoue.

Si vous souhaitez vérifier une valeur vide, 0 valeur ou des valeurs formatées non valides que vous pouvez essayer: p>

   if (editText1.getText().isEmpty()) {
     editText1.setError("Please Fill this Field");
   } else {
     try{
       float interObtain = Float.valueOf(editText1.getText());
       if (interObtain == 0 ) {
         editText1.setError("Value should be different from 0");
       }
     } catch(NumberFormatException ex){
       editText1.setError("Value has an invalid format");
     }
   }


0 commentaires

0
votes
private boolean isEditTextEmpty(EditText editText) {
    return editText.getText().toString().trim().length() == 0;
}
This is a method you could implement which returns true, if the editText is empty, it`s better than checking the float value.

0 commentaires

0
votes

éditext1.gettext (). Tostring () est renvoyé "" "Valeur lorsque EditText est vide ou il ne peut pas convertir dans le numéro de flotteur afin qu'il ne donne une erreur

Vous pouvez vérifier avec ce code P>

if(editText1.getText().toString().equalsIgnoreCase(""){
// show your error here
}


0 commentaires