【问题标题】:Android Studio if input is Decimal App crashesAndroid Studio 如果输入是十进制应用程序崩溃
【发布时间】:2020-01-20 20:43:46
【问题描述】:

我正在编写一个可以计算成绩的应用程序。

但我有一个问题:

如果 Edittext 中的 Input 是十进制,则应用程序崩溃。如果数字不是十进制,它不会崩溃并给我正确的结果。

这是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final Button berechnenButton = (Button) findViewById(R.id.berechnenButton);

    final EditText note1 = (EditText) findViewById(R.id.note1);

    final EditText note2 = (EditText) findViewById(R.id.note2);

    final EditText note3 = (EditText) findViewById(R.id.note3);

    final EditText note4 = (EditText) findViewById(R.id.note4);

    final EditText wunsch = (EditText) findViewById(R.id.schnitt);

    final TextView ausgabe = (TextView) findViewById(R.id.ausgabe);



    note1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    note2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    note3.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    note4.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    wunsch.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    ausgabe.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);









    berechnenButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {





            if (note1.getText().toString().isEmpty()) {

                note1.setError("Dieses Feld darf nicht leer sein!");

            } else {


                if (note2.getText().toString().isEmpty()) {

                    note2.setError("Dieses Feld darf nicht leer sein!");

                } else {

                    if (wunsch.getText().toString().isEmpty()) {

                        wunsch.setError("Dieses Feld darf nicht leer sein!");

                    } else {

                        double note11 = Integer.parseInt(note1.getText().toString());
                        double note22 = Integer.parseInt(note2.getText().toString());
                        double wunsch1 = Integer.parseInt(wunsch.getText().toString());


                        if (note11 > 6) {

                            note1.setError("Die Note darf nicht grösser als 6 sein!");

                        } else {

                            if (note22 > 6) {

                                note2.setError("Die Note darf nicht grösser als 6 sein!");

                            } else {

                                if (wunsch1 > 6) {

                                    wunsch.setError("Die Note darf nicht grösser als 6 sein!");

                                } else {



                                    if (note3.getText().toString().isEmpty() && note4.getText().toString().isEmpty()) {




                                        double res = (-note11-note22+wunsch1*3);

                                        ausgabe.setText(String.valueOf(res));

                                    }


                                    if (!note3.getText().toString().isEmpty() && note4.getText().toString().isEmpty()) {

                                        double note33 = Integer.parseInt(note3.getText().toString());


                                        if (note33 > 6) {

                                            note3.setError("Die Note darf nicht grösser als 6 sein!");

                                        } else {



                                            double res = (-note11-note22-note33+wunsch1*4);

                                            ausgabe.setText(String.valueOf(res));


                                        }


                                    }


                                    if (!note3.getText().toString().isEmpty() && !note4.getText().toString().isEmpty()) {

                                        double note33 = Integer.parseInt(note3.getText().toString());
                                        double note44 = Integer.parseInt(note4.getText().toString());






                                        if (note33 > 6) {

                                            note3.setError("Die Note darf nicht grösser als 6 sein!");

                                        } else {

                                            if (note44 > 6) {

                                                note4.setError("Die Note darf nicht grösser als 6 sein!");

                                            } else {




                                                double res = (-note11-note22-note33-note44+wunsch1*5);

                                                ausgabe.setText(String.valueOf(res));

                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }
    });
}
}

如果有人知道我做错了什么,请写在下面! 我将非常感激,因为我对编程很陌生! :) 提前致谢!

问候 达里奥 C.

【问题讨论】:

  • 顺便说一句,你真的需要考虑重组你的 if else 语句,在你的情况下,将太多 if else 分支放在彼此内部是非常糟糕的做法(但对于初学者来说还不错)你应该考虑写回报;在一个动作(无论应该发生什么)完成后,至少有一半的 if else 分支会消失。 Return 只是停止当前方法

标签: java android crash android-edittext decimal


【解决方案1】:

您确实应该在问题中提供崩溃信息(异常),因为这样更容易找到。

浏览您的代码后,我很确定崩溃的原因是这样的:

double note33 = Integer.parseInt(note3.getText().toString());
double note44 = Integer.parseInt(note4.getText().toString());

当 String 无法转换为 int 时,Integer.parseInt 会引发异常,在您的情况下可以转换 3,但像 3.345 这样的数字不是整数,因此它们会导致崩溃

要解决这个问题,只需将所有这些调用替换为:

Double.parseDouble(note3.getText().toString())

左边已经正确了

M.f.G:D

【讨论】:

  • 嗨默桑!这是我的问题的解决方案,非常感谢!终于我的应用程序完成了!制造:D
  • 没问题,你能把我的答案标记为解决方案吗?顺便说一句,没有理由在左侧使用“Double”而不是“double”
【解决方案2】:

解决办法是这样的:

你必须替换这个:

double note33 = Integer.parseInt(note3.getText().toString());

与:

Double note33 = Double.parseDouble(note3.getText().toString())

感谢 Merthan 的解决方案!

【讨论】:

  • 可能没有理由在左侧使用“Double”而不是“double”(双大写有一些缺点,请查看装箱/装箱数据类型)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2016-01-14
  • 2017-07-22
相关资源
最近更新 更多