我该如何修复numberformatexception

时间:2015-08-20 06:08:53

标签: java android

我是android和java编程的新手,我知道这个错误是由数字格式异常引起的,但我不知道如何修复它。但我是新人,所以我很抱歉这是一个如此低级别的问题。

public class MainActivity extends Activity {
        public double x = 0;
        public double y = 0;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //Button
            Button btn = (Button) findViewById(R.id.button);

            //EditText
            EditText nop = (EditText) findViewById(R.id.editText);
            EditText cob = (EditText) findViewById(R.id.editText2);

            x = Double.parseDouble(nop.getText().toString());
            y = Double.parseDouble(cob.getText().toString());

            try{
                x = Double.valueOf(nop.getText().toString());
            } catch (NumberFormatException e) {
                x = 0;
            }

            try{
                y = Double.valueOf(cob.getText().toString());
            } catch (NumberFormatException e) {
                y = 1;
            }


            //TextView
            final TextView tv = (TextView) findViewById(R.id.textView);

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                     Double z = x / y;
                    tv.setText("Result:" + Double.toString(z));
                }
            });

        }
        }

这是Logcat:

08-20 01:03:42.951  18608-18608/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
08-20 01:03:43.215  18608-18608/com.elie.billsplitter D/AndroidRuntime﹕ Shutting down VM
08-20 01:03:43.258  18608-18608/com.elie.billsplitter E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.elie.billsplitter, PID: 18608
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elie.billsplitter/com.elie.billsplitter.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NumberFormatException: Invalid double: ""
            at java.lang.StringToReal.invalidReal(StringToReal.java:63)
            at java.lang.StringToReal.parseDouble(StringToReal.java:267)
            at java.lang.Double.parseDouble(Double.java:301)
            at com.elie.billsplitter.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-20 01:03:46.732  18608-18608/com.elie.billsplitter I/Process﹕ Sending signal. PID: 18608 SIG: 

9

3 个答案:

答案 0 :(得分:1)

它发生的原因是你试图获取edittext的值并将其转换为double。问题是你试图获取空的editext值并将其转换为double,因此它会给你带来无效的双“”错误。

如果您想解决问题,请在需要时获取edittext值并将其检查为空。

btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     if(!nop.getText().toString().isEmpty() && !cob.getText().toString().isEmpty())
                     {
                          x = Double.parseDouble(nop.getText().toString()); //Assign value to x here not after the EditText declaration
                          y = Double.parseDouble(cob.getText().toString()); // Assign value to y here not after the EditText declaration
                          Double z = x / y;
                          tv.setText("Result:" + Double.toString(z));
                     }
                     else
                     { 
                          // Show appropriate message for enter the value
                     }  

                }
            });

答案 1 :(得分:0)

x = Double.parseDouble(nop.getText().toString());
y = Double.parseDouble(cob.getText().toString());

在try块之外不需要这两行。你已经在try块中拥有它们了。将它们放在外面会使它容易抛出异常。

Button btn = (Button) findViewById(R.id.button);
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);

double x = 0;
double y = 1;
try{
  x = Double.valueOf(nop.getText().toString());
  } catch (NumberFormatException e) {
      }
try{
   y = Double.valueOf(cob.getText().toString());
   } catch (NumberFormatException e) {

    }

答案 2 :(得分:0)

您还使用try-catch块包装解析代码。

        try{
            x = Double.parseDouble(nop.getText().toString());
            x = Double.valueOf(nop.getText().toString());
        } catch (NumberFormatException e) {
            x = 0;
        }

        try{
            y = Double.parseDouble(cob.getText().toString());
            y = Double.valueOf(cob.getText().toString());
        } catch (NumberFormatException e) {
            y = 1;
        }

此外,我不确定为什么你需要这两行。

        y = Double.parseDouble(cob.getText().toString());
        y = Double.valueOf(cob.getText().toString());

(在x的情况下也是如此)。正如@Uma kanth所说,你可以删除其中一个