Olá pessoal estou fazendo um aplicativo no android studio mas não sei exatamente como verificar se os campos do texto estão vazio, tentei a seguinte solução no código abaixo :
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");
}
}
}
Mais ao clicar no executar com os campos vazios recebo o seguinte erro:
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)