0
public class VigMetBiapsb extends Activity {
int porctAlt, porctLarg;
double edtVaoNum;
EditText edtVao;
Button calcBiapsb;
TextView secaoBiapsb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vigmet_biapsb);
edtVao = (EditText) findViewById(R.id.vao);
calcBiapsb = (Button) findViewById(R.id.calc_biapsb);
porctAlt = 6;
porctLarg = 60;
calcBiapsb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edtVaoNum = Double.parseDouble(edtVao.getText().toString());
if(edtVao.getText().toString().equals("")){
secaoBiapsb = (TextView) findViewById(R.id.valorsec);
secaoBiapsb.setText("Dado inválido!");
}
else {
double alt = edtVaoNum * porctAlt;
double larg = alt / 100 * porctLarg;
secaoBiapsb = (TextView) findViewById(R.id.valorsec);
secaoBiapsb.setText(String.valueOf(alt)+" x "+String.valueOf(larg));
}
}
});
} //fecha onCreate
}
If I fill the edtVao
, everything is executed correctly, but if I leave the edtVao
empty, the application closes when I touch the button calcBiapsb
.
The excerpt edtVao.getText().toString().equals("")
within the if
shouldn’t avoid this? How could I check whether the field is empty or not? (remembering that what is received from the field is passed to a variable double
)
Fixed moving this line inside the
else
. Thank you!– EdeiltonSO