空 EditText Android 崩溃
@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 的按钮时,应用程序崩溃。
谢谢。
@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");
}
}
Any idea how to fix this? When I click on the button with empty EditText, the application crashes.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的整数解析既低效又损坏。同样的事情你做了两次。每当你两次编写完全相同的代码时,你应该问问自己是否做错了。
试试这个:
另外,就像我在评论中提到的那样,您在比较中缺少
=
(if(usableInt = false
))。Your integer parsing is both inefficient and broken. You're doing the same thing twice. Whenever you write the exact same code twice, you should ask yourself if you're doing it wrong.
Try this:
Also, like I mentioned in a comment, you're missing a
=
in the comparison (if(usableInt = false
).您有一个 try/catch 围绕 EditText 的文本值上对 parseInt 的直接调用,但稍后您会再次执行此操作。这可能是您抛出异常的地方。
You have one try/catch surrounding a direct call to parseInt on the EditText's text value, but then later on you do it again. This is probably where you're throwing an exception.