Check if EditText is empty

1
Hello everyone, I'm doing an application in android studio but I do not know exactly how to check if the text fields are empty, I tried the following solution in the code below:

public class MainActivity extends AppCompatActivity {

EditText edt1,edt2;
TextView tv3;
float n1 = 0.70f;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edt1 = (EditText) findViewById(R.id.editText);
    edt2 = (EditText) findViewById(R.id.editText2);
    tv3 = (TextView) findViewById(R.id.textView3);

}


public void calcular(View v){

    double valor1 = Double.parseDouble(edt1.getText().toString());
    double valor2 = Double.parseDouble(edt2.getText().toString());


    double resultado1 = valor1 * n1;

    if (edt1.getText().toString().equals("")){
       tv3.setText("Campo vazio");
    }

    if (edt2.getText().toString().equals("")){
       tv3.setText("Campo vazio");
    }

    if (resultado1 < valor2) {
        tv3.setText("Vale a pena Abastecer com Gasolina");
    }


    else {
        tv3.setText("Vale a pena Abastecer com Alcool");
    }


    }

}

More when I click on the run with the empty fields I get the following error:

08-08 15:21:53.288 25012-25012/com.dolardehoje.formulario W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a19930)
08-08 15:21:53.298 25012-25012/com.dolardehoje.formulario E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
        at android.view.View.performClick(View.java:4380)
        at android.view.View$PerformClick.run(View.java:18094)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:4380) 
        at android.view.View$PerformClick.run(View.java:18094) 
        at android.os.Handler.handleCallback(Handler.java:725) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5279) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.NumberFormatException: Invalid double: ""
        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
        at java.lang.StringToReal.parseDouble(StringToReal.java:248)
        at java.lang.Double.parseDouble(Double.java:295)
        at com.dolardehoje.formulario.MainActivity.calcular(MainActivity.java:38)
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
        at android.view.View.performClick(View.java:4380) 
        at android.view.View$PerformClick.run(View.java:18094) 
        at android.os.Handler.handleCallback(Handler.java:725) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5279) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 
    
asked by anonymous 08.08.2016 / 16:01

6 answers

6

The error happens because you are trying to get a Double of a String that is null or has no convertible value.

Change the code so that the EditText is validated before getting the values in Double

public void calcular(View v){

    if (edt1.getText().toString().trim().equals("") ||
        edt2.getText().toString().trim().equals("")){

       Toast.makeText(this, "Valores incorretos",Toast.LENGTH_LONG).show();

    }else{
        double valor1 = Double.parseDouble(edt1.getText().toString());
        double valor2 = Double.parseDouble(edt2.getText().toString());

        double resultado1 = valor1 * n1;

        if (resultado1 < valor2) {
            tv3.setText("Vale a pena Abastecer com Gasolina");
        }
        else {
            tv3.setText("Vale a pena Abastecer com Alcool");
        }
    }
}

To ensure that the user only enters valid values (numbers) add the following attributes to EditText edt1 and edt2

android:text="0" 
android:inputType="numberDecimal"
    
08.08.2016 / 18:48
2

If you just want to check if the field is empty try using the following line:

if(edt1.getText().toString().equals("")){
   //Colocar o que vc quer que aconteça caso esteja vazio.
}

or so:

if(!edt1.getText().toString().equals("")){
   //Colocar o que vc quer que aconteça caso não esteja vazio.
}

I'm basically picking the field and picking up what's in it. And with .equal ("") I am comparing it to an empty field.

I hope I have helped.

    
08.08.2016 / 16:07
0

So you can check this way by creating the following function, which will check if you have white space and if the string size is less than 1.

private boolean isEmpty(EditText etText) {
        String text = etText.getText().toString().trim();
        if (text.length()<1)
            return true;
        return false;
}
    
08.08.2016 / 19:31
0

"thread exiting with uncaught exception"

declare value1 and global2 value and use it here:

try
{
    valor1 = Double.parseDouble(edt1.getText().toString());
    valor2 = Double.parseDouble(edt2.getText().toString());
} 
catch(NumberFormatException e)
{
    tv3.setText("Valor Invalido");
}
    
09.08.2016 / 03:05
0

Based on what the ramaral did, because this is correct, I would only change a few points and would add some line for the code to be more complete.

public void calcular(View v){

   if (edt1.getText().toString().trim().equals("") &&
       edt2.getText().toString().trim().equals("")){

       Toast.makeText(this, "Os dois campos estao vazios!",Toast.LENGTH_LONG).show();

   }else if(edt1.getText().toString().trim().equals("")){

       Toast.makeText(this, "O primeiro campo esta vazio",Toast.LENGTH_LONG).show();

   }else if(edt2.getText().toString().trim().equals("")){

       Toast.makeText(this, "O segundo campo esta vazio",Toast.LENGTH_LONG).show();

   }else{

       double valor1 = Double.parseDouble(edt1.getText().toString());
       double valor2 = Double.parseDouble(edt2.getText().toString());

       double resultado1 = valor1 * n1;

       if (resultado1 < valor2) {
           tv3.setText("Vale a pena Abastecer com Gasolina");

       }else {
           tv3.setText("Vale a pena Abastecer com Alcool");
       }
   }
}

I know there are few changes, but I hope I have helped

    
09.08.2016 / 12:14
0

You can do this:

if (edt1.getText().toString().isEmpty()) {

   //Informar que o campo 1 está vazio.

}
    
09.08.2016 / 12:26