[英]NumberFormatingExcepton if App is launched First Time
I have 4 TextView
s.我有 4 个
TextView
。 3 of them display results of EditText
input value in another activity, and it's saved with shared preferences.其中 3 个在另一个活动中显示
EditText
输入值的结果,并与共享首选项一起保存。
The fourth TextView
needs to display sum of that previous 3 TextView
s.第四个
TextView
需要显示前 3 个TextView
的总和。
I get the error: NumberFormatingException invalid double ""
我收到错误:
NumberFormatingException invalid double ""
This is my code:这是我的代码:
// GETTING VALUES FOR FIRST 3 TEXTVIEWS
textViewRezultat1RMPotisakSKlupe.setText(settings.getString("benchSave", null));
textViewRezultat1RMCucanj.setText(settings.getString("squatSave", null));
textViewRezultat1RMMrtvoDizanje.setText(settings.getString("deadSave", null));
//LITTLE BIT OF MATH TO GET VALUE OF FOURTH
double prvo = Double.parseDouble(textViewRezultat1RMPotisakSKlupe.getText().toString());
double drugo = Double.parseDouble(textViewRezultat1RMCucanj.getText().toString());
double trece = Double.parseDouble(textViewRezultat1RMMrtvoDizanje.getText().toString());
double rezultat = 0;
rezultat = (prvo + drugo + trece);
textViewRezultat1RMUkupno.setText(Double.toString(rezultat));
That works like a charm.这就像一个魅力。
The problem is, I get the error I mentioned above if the app is launched for the first time (with no stored data).问题是,如果应用程序是第一次启动(没有存储数据),我会收到上面提到的错误。
Can anyone help me solve it?有人可以帮我解决吗?
Actually the problem occurs when trying to parse a double from a string that is not representing a number or just simply null.实际上,当尝试从不表示数字或只是 null 的字符串中解析 double 时会出现问题。
public static double parseDouble(String s) throws NumberFormatException
public static double parseDouble(String s) 抛出 NumberFormatException
Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
返回一个新的 double ,初始化为由指定的 String 表示的值,由 Double 类的 valueOf 方法执行。
Parameters : s - the string to be parsed.
参数: s - 要解析的字符串。
Returns : the double value represented by the string argument.
返回: 字符串参数表示的双精度值。
Throws :
抛出:
NullPointerException - if the string is null
NullPointerException - 如果字符串为空
NumberFormatException - if the string does not contain a parsable double.
NumberFormatException - 如果字符串不包含可解析的双精度值。
So you need to test both scenarios to be sure you are not in either of the faulty cases.因此,您需要测试这两种情况,以确保您没有遇到任何一种错误情况。
String prvoString = textViewRezultat1RMPotisakSKlupe.getText().toString();
Double prvo;
if(prvoString != null) { //shouldn't occur
try {
prvo = Double.parseDouble(prvoString);
} catch (NumberFormatException ex) { //inputs like "" or "banana"
//Tell the user he is entering invalid input
}
}
So why is this occuring when your app is running for the first time?那么为什么在您的应用程序第一次运行时会发生这种情况呢? The code that is parseDouble is executed when your view is loaded and trying to parse empty ("") strings.
当您的视图加载并尝试解析空 ("") 字符串时,将执行 parseDouble 代码。
Before doing double prvo = Double.parseDouble()
always check whether your Strings are valid or not because if your String is empty, that is like ""
, you can't convert this to Double as its not a number.在执行
double prvo = Double.parseDouble()
始终检查您的 Strings 是否有效,因为如果您的 String 为空,例如""
,则无法将其转换为 Double ,因为它不是数字。
So before every Double.parseDouble
check like this -所以在每次
Double.parseDouble
检查之前 -
if (!TextUtils.isEmpty(yourString))
Double.parseDouble(yourString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.