I want to make a calculator that uses the button Add to take 2 different EditText's and add them, showing on a Toast. But when I click directly on the button without typing any value, the app crashes. I would be very glad to hear a solution to this. I tried with an if comparing with null. Here's the code:
if(num1 != null && num2 != null){
som.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v){
Double valor1 = Double.parseDouble(num1.getText().toString());
Double valor2 = Double.parseDouble(num2.getText().toString());
Double Soma = valor1+valor2;
Toast.makeText(MainActivity.this,"A soma é: "+Soma, Toast.LENGTH_LONG).show();
}
});
}
put if(! num1 .getEditableText().toString().matches("") && !num2 .getEditableText().toString().matches(""))
in your onClick method