1错误不会让我的代码编译

时间:2015-08-19 14:34:15

标签: java android

我是Android编程的新手,我正在尝试创建一个在人与人之间分配账单的应用程序。但关于我的整数的一些事情是错误的。我知道这是因为在我的logcat中它说"引起:java.lang.NumberFormatException:无效的int:""

这是我的MainActivity.java

public class MainActivity extends Activity {
public double x;
public double y;
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);


    //Getting the strings
    try{
        x = Double.valueOf(nop.getText().toString());
    } catch (NumberFormatException) {
        x = 0;
    }

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

    String textX = nop.getText().toString();
// if variable contains valid number:
    if (textX != null && textX.length() > 0)
        x = Double.valueOf();   // exception thrown by method
    else
        x = 0;

    //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((int) z);
        }
    });

}
}

这是logcat:

08-19 07:30:28.452  16070-16070/com.elie.billsplitter E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.elie.billsplitter, PID: 16070
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elie.billsplitter/com.elie.billsplitter.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
            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 int: ""
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parseInt(Integer.java:358)
            at java.lang.Integer.parseInt(Integer.java:334)
            at com.elie.billsplitter.MainActivity.onCreate(MainActivity.java:25)
            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)

1 个答案:

答案 0 :(得分:1)

展望你的异常:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.elie.billsplitter / com.elie.billsplitter.MainActivity}: java.lang.NumberFormatException:无效的int:""

错误出现在这段代码中

//Getting the strings
x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());

为什么?
nop.getText().toString().equals("")cob.getText().toString().equals("")

如何避免?
您必须确保从此String获得有效的EditText,因为它们是空的或不包含等效的double { {1}} String您将拥有NumberFormatException

解决方案?
最佳方式是:

  • EditText添加验证以仅获得有效的String
  • 如果抛出NumberFormatException,则转换该值。

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

try{
    y = Double.valueOf(cop.getText().toString());
} catch (NumberFormatException e) {
    y = 1; // division / 0 will throw NaN
}

添加-ON: 为什么我不使用Integer.parseInt() ??
如果您创建double,则无法解析int,因为您将失去准确性,例如在java (int) 1 / (int) 3 = 0(int) 10 / (int) 3 = 3中,请使用Double::valueOf()

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