简体   繁体   English

NumberFormatingExcepton 如果应用程序是第一次启动

[英]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 时会出现问题。

parseDouble 解析双

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.

相关问题 MediaPlayer - 如何在第一次启动应用程序时显示警报对话框 - MediaPlayer - How to show a alert dialoge when app is launched first time 该应用程序如何在首次启动时打开一个不同的活动,然后又打开另一个活动? - How to have the app open a different activity for the first time it is launched and a different one from then on? Android Maps v2仅在首次启动时有效 - Android Maps v2 works only the first time is launched 哪个应用启动了我的应用? - Which app launched my app? 第二次启动应用程序时,是否可以使用 SharedPreferences 更改 WebView 的默认 loadUrl? - Is it possible to use SharedPreferences to change the default loadUrl of WebView when the app is launched the second time? 仅在应用程序启动时显示启动画面,而不是每次调用包含启动画面代码的活动时显示启动画面 - Display splash screen only when the app is launched and not every time when the activity containing splash screen code, is called 检测并获取启动的应用程序的名称 - Detect and get name of an app launched android gps应用程序仅在第一次工作 - android gps app just working first time 首次应用启动时再次加载活动 - Activity load again in first time app Launch 首次在 android 应用程序中显示条款和条件 - Showing Terms and Conditions for first time in an android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM