android - 当用户未插入任何值时,我的应用程序崩溃了 android studio

标签 android crash try-catch

我尝试使用 try/catch 方法来防止应用程序在用户没有提供任何值时崩溃,即使使用此 try catch,它仍然显示错误。

提前感谢那些愿意回复的好心人

    editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.textView);
}

public void btnSubmit_OnClick(View view) {

    try {
        double num = Double.parseDouble(editText.getText().toString());

    }catch (Exception e) {
         Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    if (num >= 6.5 && num < 14) {

            textView.setText("You may have Diabetes or if you are already diagnosed, have Uncontrolled Diabetes. However, I still find it best that you contact your attending physician for proper assessment. Thank you.");


        } else if (num >= 14){
            textView.setText("This is the highest level the application can read. Please contact your attending physician as either there is an error or you are suffering from a condition that warrants immediate referral. Thank you.");

最佳答案

只需在 try-catch 之外声明 num 即可。

double num=0.0;
try {
    num = Double.parseDouble(editText.getText().toString());
}catch (Exception e) {
    Toast.makeText(MainActivity.this, e.getMessage(), 
                 Toast.LENGTH_SHORT).show();
}

if (num >= 6.5 && num < 14) {

     ...

关于android - 当用户未插入任何值时,我的应用程序崩溃了 android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49310304/

相关文章:

ios - 从数据创建 UIImage 时崩溃

java - NullPointerException 无法解析为变量

flutter - Dart 中有没有办法将函数标记为抛出异常?

android - 已发布错误通知 - 无法为 : StatusBarNotification 展开 RemoteViews

android - 降低 ORMlite 的内部日志详细程度或禁用它

android - 从 EditText 设置日期选择器对话框的日期

android - 为什么应用程序在尝试选择文本时会崩溃?

android - 错误: Audio Priority Boost

iphone - 应用程序因在第三代 iPad 上崩溃而被拒绝

java - 在 For 循环中使用 Try/Catch