NumberFormatException错误 [英] NumberFormatException Error

查看:197
本文介绍了NumberFormatException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是:

editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);
float from_value= Float.parseFloat(editText2.getText().toString());
editText3.setText(" "+(from_value * 100.0));

并且logcat错误是:

And the logcat error is:


03-18 03:19:07.847:E / AndroidRuntime(875):引起:java.lang.NumberFormatException:无效的浮点数:

03-18 03:19:07.847: E/AndroidRuntime(875): Caused by: java.lang.NumberFormatException: Invalid float: ""


推荐答案

似乎editText2中的String为空,因此无法将其解析为float。

Seems like the String in editText2 is empty, so it fails to parse it as float.

一个可能的解决方案是首先检查String是否为空,然后确定默认值,另一个是捕获异常:

A possible solution is to check if the String is empty first, and then decide about default value, another is to catch the exception:

float from_value;
try {
    from_value = Float.parseFloat(editText2.getText().toString());
}
catch(NumberFormatException ex) {
    from_value = 0.0; // default ??
}

这篇关于NumberFormatException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆