0

Im using this code to get value from a string and then posting it after performing operation.

The problem is that after entering some value when I try to erase the entire text field it stops unexpectedly.Im using onTextChanged method and the piece of code that Im working with is here. Also it works fine when I use a Button to submit instead on textWatcher.

min=(EditText) findViewById(R.id.minutes);
sec=(EditText) findViewById(R.id.seconds);
millisec=(EditText) findViewById(R.id.milliseconds);
String minute = min.getText().toString();
Double   min1=Double.parseDouble(minute);
            min1=min1*60;
Double  min2=min1*1000;
sec.setText(Double.toString(min1));
millisec.setText(Double.toString(min2));
4

1 回答 1

2

如果您不传递可解析的字符串,则 parseDouble 将抛出 NumberFormatException ......因此,如果您清空该字段,您将收到异常。

您可以将代码包装在 try-catch-clause 中:

String minute = min.getText().toString();

try{
   Double min1 = Double.parseDouble(
   min1=min1*60;

   Double min2=min1*1000;
   sec.setText(Double.toString(min1));
   millisec.setText(Double.toString(min2));
} catch(NumberFormatException e){
   sec.setText("");
   millisec.setText("");
} 
于 2013-09-10T14:28:58.027 回答