2012-04-24 66 views
0

在我的项目之前,我有一个按钮,计算在至少2编辑框,但是当我离开它空的,计算我崩溃的应用程序:如何检查空的EditText计算

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.math.square.Level1.calculate(Level1.java:71) 
at com.math.square.Level1.access$0(Level1.java:66) 
at com.math.square.Level1$2.onClick(Level1.java:62) 
at android.view.View.performClick(View.java:3511) 
at android.view.View$PerformClick.run(View.java:14105) 
at android.os.Handler.handleCallback(Handler.java:605) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4424) 
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:784) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
at dalvik.system.NativeStart.main(Native Method) 

我能做些什么在计算之前检查,如果它是空的,提醒用户并且不计算?

代码:

public class Level1 extends Activity { 
    EditText amount1; 
    EditText amount2; 
    EditText amount3; 
    EditText amount4; 
    Button calculate; 
    double a=0; 
    double b=0; 
    double c=0; 
    double d=0; 
    double e=0; 
    double f=0; 
    double g=0; 
    double h=0; 

    /** Called when the activity is first created. */ 
    @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.level1); 

      initControls(); 

     findViewById(R.id.Button05).setOnClickListener(
       new OnClickListener() { 
      public void onClick(View v) { 
       btn05(true); 
      } 
     });; 
     } 

      private void initControls() 
      { 
       amount1=(EditText)findViewById(R.id.amount1); 
       amount2=(EditText)findViewById(R.id.amount2); 
       amount3=(EditText)findViewById(R.id.amount3); 
       amount4=(EditText)findViewById(R.id.amount4); 
       calculate=(Button)findViewById(R.id.calculate); 
       calculate.setOnClickListener(new Button.OnClickListener() 
       { 
        public void onClick 
       (View v) { calculate();} 
       }); 
      } 

     private void calculate() 
      { 
       a=Double.parseDouble(amount1.getText().toString()); 
       b=Double.parseDouble(amount2.getText().toString()); 
       d=Double.parseDouble(amount3.getText().toString()); 
       e=Double.parseDouble(amount4.getText().toString()); 

       /** Check Value > 0*/ 
       EditText amount1value = (EditText)this.findViewById(R.id.amount1); 
       EditText amount2value = (EditText)this.findViewById(R.id.amount2); 
       EditText amount3value = (EditText)this.findViewById(R.id.amount3); 
       EditText amount4value = (EditText)this.findViewById(R.id.amount4); 
       Integer nameLen = 2; 

       int len = amount1value.length(); 
       int len2 = amount2value.length(); 
       int len3 = amount3value.length(); 
       int len4 = amount4value.length(); 

       if (len<nameLen) { 
        amount1value.setError(getText(R.string.blank)); 
        TextView Result = (TextView)findViewById(R.id.Result); 
        Result.setText(R.string.error_character_length); 
       } else 
       if (len2<nameLen) { 
        amount2value.setError(getText(R.string.blank)); 
        TextView Result = (TextView)findViewById(R.id.Result); 
        Result.setText(R.string.error_character_length); 
       } else 
       if (len3<nameLen) { 
        amount3value.setError(getText(R.string.blank)); 
        TextView Result = (TextView)findViewById(R.id.Result); 
        Result.setText(R.string.error_character_length); 
       } else 
       if (len4<nameLen) { 
        amount4value.setError(getText(R.string.blank)); 
        TextView Result = (TextView)findViewById(R.id.Result); 
        Result.setText(R.string.error_character_length); 
       } 

       else { 

       c=a+b; 
       f=d+e; 
       g=a+d; 
       h=b+e; 

       if(c==9 && f==12 && g==8 && h==13){ 
       TextView Result = (TextView)findViewById(R.id.Result); 
       Result.setText(R.string.correct); 
       } 
       else { 
       TextView Result = (TextView)findViewById(R.id.Result); 
       Result.setText(R.string.wrong); 
       } 
       } 
      } 

我创建了一个检查,看看是否有2个字符,以尽量避免空白的用户,但它只是一种变通方法。

回答

1

答案很简单。在运行计算之前,请先检查EditText以获取文本。如果它为空,则显示一个错误的敬酒。否则,继续计算。另外需要注意的是,您的EditText应该只接受数字作为输入类型。不需要尝试捕捉。有关详细信息,请查看dev站点上的EditText文档。

+0

我试图在计算之前检查EditText,但是做错了什么,有没有代码的任何想法来帮助我呢?这个尝试的目的是在计算前检查EditText上至少2个字符的长度,如果我用空的EditText修复这个问题,我可以删除这个try catch。 – VitorEuphrasio 2012-04-25 12:13:05

+0

明白了!解决了我的问题! – VitorEuphrasio 2012-04-25 14:38:46

0

围绕您的parseDouble方法在try-catch,捕获NumberFormatException。如果发生异常,请通知用户他们输入的值无效,并且不要继续计算。