2
Hello guys I’m making an app on android studio but I don’t 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");
}
}
}
Further by clicking 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)
Possible duplicate of How to test if an Edittext is empty?
– ramaral
Try it this way: Textutils.isEmpty(edt1.gettext(). toString())
– Thiago Luiz Domacoski
Behold How to consider an empty Edittext as "0"
– ramaral