简体   繁体   中英

eclipse - android application crashes when EditText is empty

Good day. I wrote application in eclipse for android and when EditText is empty application crashes. I try so much ways but it do not work. Every it crash when i enter it and EditText is emty. Sorry for my english. This is part of my code :

public class MainActivity extends Activity implements OnClickListener {

TextView mnozstvo;
TextView percento;
TextView hmotnost;
TextView vysledok1;
TextView vysledok2;
TextView text;
RadioButton muz1;
RadioButton zena1;
Button vysledok;
EditText coo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView)this.findViewById(R.id.text);

mnozstvo = (TextView)this.findViewById(R.id.dl);


percento = (TextView)this.findViewById(R.id.per);

hmotnost = (TextView)this.findViewById(R.id.kg);

vysledok1 = (TextView)this.findViewById(R.id.krv);

vysledok2 = (TextView)this.findViewById(R.id.dych);

muz1 = (RadioButton)this.findViewById(R.id.muz);

zena1 = (RadioButton)this.findViewById(R.id.zena);

vysledok = (Button)this.findViewById(R.id.vysledok);

vysledok.setOnClickListener(this);


}
.................
public void convertDollarsToEuros() {
double val = Double.parseDouble(mnozstvo.getText().toString());
double val1 = Double.parseDouble(percento.getText().toString());
double val2 = Double.parseDouble(hmotnost.getText().toString());

vysledok1.setText(Double.toString((val*val1*0.8)/(val2*0.68)));
vysledok2.setText(Double.toString(((val*val1*0.8)/(val2*0.68))/(2.1)));

text.setText(Double.toString(((val*val1*0.8)/(val2*0.68))*6.6164));



}

public void convertEurosToDollars() {

double val = Double.parseDouble(mnozstvo.getText().toString());
double val1 = Double.parseDouble(percento.getText().toString());
double val2 = Double.parseDouble(hmotnost.getText().toString());

vysledok1.setText(Double.toString((val*val1*0.8)/(val2*0.55)));
vysledok2.setText(Double.toString(((val*val1*0.8)/(val2*0.55))/(2.1)));
text.setText(Double.toString(((val*val1*0.8)/(val2*0.55))*6.6164));

Can someone help me?

Double.parseDouble() will raise a NumberFormatException.

But instead of waiting for an Exception to happen and then handling it you can check for the length of the string before trying to parse it.

Your code should look something like this:

public void convertEurosToDollars() {
    double val = 0;
    double val1= 0;
    double val2= 0;
    if (mnozstvo.getText().length() > 0 ) {
        val = Double.parseDouble(mnozstvo.getText().toString());
    }
    if (percento.getText().length() > 0 ) {
        val2 = Double.parseDouble(percento.getText().toString());
    }
    if (hmotnost.getText().length() > 0 ) {
        val3 = Double.parseDouble(hmotnost.getText().toString());
    }       


    vysledok1.setText(Double.toString((val*val1*0.8)/(val2*0.55)));
    vysledok2.setText(Double.toString(((val*val1*0.8)/(val2*0.55))/(2.1)));
    text.setText(Double.toString(((val*val1*0.8)/(val2*0.55))*6.6164));
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM