Android Studio 如果输入是十进制应用程序崩溃
Android Studio if input is Decimal App crashes
我正在编写一个可以计算成绩的应用程序。
但是我有一个问题:
如果 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());
Integer.parseInt 当 String 无法转换为 int 时抛出异常,在您的情况下 3 可以转换,但像 3.345 这样的数字不是 int,因此它们会导致崩溃
要解决这个问题,只需将所有这些调用替换为:
Double.parseDouble(note3.getText().toString())
左边已经正确了
M.f.G:D
解决方案是这样的:
你必须替换这个:
double note33 = Integer.parseInt(note3.getText().toString());
与:
Double note33 = Double.parseDouble(note3.getText().toString())
感谢 Merthan 提供解决方案!
我正在编写一个可以计算成绩的应用程序。
但是我有一个问题:
如果 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());
Integer.parseInt 当 String 无法转换为 int 时抛出异常,在您的情况下 3 可以转换,但像 3.345 这样的数字不是 int,因此它们会导致崩溃
要解决这个问题,只需将所有这些调用替换为:
Double.parseDouble(note3.getText().toString())
左边已经正确了
M.f.G:D
解决方案是这样的:
你必须替换这个:
double note33 = Integer.parseInt(note3.getText().toString());
与:
Double note33 = Double.parseDouble(note3.getText().toString())
感谢 Merthan 提供解决方案!