我尝试使用 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/