Java 如何修复numberformatexception
我是android和java编程新手,我知道这个错误是由数字格式异常引起的,但我不知道如何修复它。但我是新来的,所以很抱歉这是一个很低级的问题Java 如何修复numberformatexception,java,android,Java,Android,我是android和java编程新手,我知道这个错误是由数字格式异常引起的,但我不知道如何修复它。但我是新来的,所以很抱歉这是一个很低级的问题 public class MainActivity extends Activity { public double x = 0; public double y = 0; protected void onCreate(Bundle savedInstanceState) { su
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));
}
});
}
}
以下是日志:
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:
九,
在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) {
}
这是因为您试图获取edittext的值并将其转换为double。问题是,您正试图获取空的EditText值并将其转换为double,所以它会给您提供无效的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
}
}
});
您还使用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所说,通过正确设置数字格式,您可以删除其中一个哪一行显示错误?如果您提供了实际的StackTrace,我们将能够给出更多更好的答案。错误就在跟踪中:无法解析为数字。@Asm通过在该字符串中添加numericalv值来传递正确的双精度值,而不是。它试图解析为数字,但不是数值,也无法解析为一。1将被解析为1,但编译器不知道如何解释为数字。在尝试解析之前,请确保该字符串中有值。如何在不中断用户输入文本的情况下使edittext包含某些内容numbers@Asm您可以使用if!检查editText是否为空!TextUtils.isEmptyeditText.getText.toString{//do something}@Raghunandan你能告诉我在做某事时该做些什么吗?因为我是新手,我不知道怎么做不,你不需要两个。第一行就足够了。
y = Double.parseDouble(cob.getText().toString());
y = Double.valueOf(cob.getText().toString());