@Override
public void onClick(View view) {
AtimesB = nrB * nrA;
try {
AtimesB = Integer.parseInt(editA.getText().toString());
}catch(NumberFormatException e){
textInC.setText("Error!");
usableInt = false;
}
if(AtimesB == Integer.parseInt(editA.getText().toString())){
editA.setText("");
textC.setTextColor(Color.parseColor("#87d9ff"));
textC.append("CORECCT: " + nrB + " x " + nrA + " = " + AtimesB + "\n");
textInC.setText("");
nrA = rand.nextInt(50)+1;
nrB = rand.nextInt(50)+1;
textA.setText(String.valueOf(nrA));
textB.setText(String.valueOf(nrB));
}else if(usableInt = false){
textInC.setTextColor(Color.RED);
textInC.setText("Error");
}else{
textInC.setText("INCORECCT");
}
}
知道怎么解决这个问题吗?当我单击带有空EditText的按钮时,应用程序崩溃。
感谢。
答案 0 :(得分:1)
在EditText的文本值上有一次try / catch围绕对parseInt的直接调用,但稍后再次执行此操作。这可能是你抛出异常的地方。
答案 1 :(得分:0)
您的整数解析既低效又破坏。你做了两次相同的事情。每当你写两次完全相同的代码时,你应该问问自己是否做错了。
试试这个:
try {
if(AtimesB == Integer.parseInt(editA.getText().toString())) {
editA.setText("");
textC.setTextColor(Color.parseColor("#87d9ff"));
textC.append("CORECCT: " + nrB + " x " + nrA + " = " + AtimesB + "\n");
[...]
}
} catch(NumberFormatException e){
textInC.setText("Error!");
usableInt = false;
}
另外,就像我在评论中提到的那样,您在比较中遗漏了=
(if(usableInt = false
)。