我正在编写一个可以计算成绩的应用程序。
但我有一个问题:
如果 Edittext 中的输入是十进制,应用程序就会崩溃。如果数字不是十进制,它不会崩溃并给出正确的结果。
代码如下:
@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.
最佳答案
您确实应该在问题中提供崩溃信息(异常),因为这样更容易找到。
浏览完您的代码后,我很确定崩溃的原因是这样的:
double note33 = Integer.parseInt(note3.getText().toString());
double note44 = Integer.parseInt(note4.getText().toString());
当 String 无法转换为 int 时,Integer.parseInt 会引发异常,在您的情况下,可以转换 3,但像 3.345 这样的数字不是 int,因此会导致崩溃
要解决此问题,只需将所有这些调用替换为:
Double.parseDouble(note3.getText().toString())
左边已经正确了
制造商:D
关于java - Android Studio 如果输入为 Decimal 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59830592/