[英]The app crashes when I click on the “Done” button on numeric keyboard
I have this code: 我有以下代码:
final EditText edC = (EditText) findViewById(R.id.etC);
final String creditsS = edC.getText().toString().trim();
final TextView tvD = (TextView) findViewById(R.id.tvDisplay);
tvD.setText("Enter amount of credits");
edC.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int credits = Integer.parseInt(creditsS);
if (actionId == EditorInfo.IME_ACTION_DONE) {
tvD.setText("Your amount of credits" + credits);
return true;
}
return false;
}
});
and when I click the "Done" button, the app crashes. 当我单击“完成”按钮时,应用程序崩溃。 I don't understand where is the problem. 我不明白问题出在哪里。 Please help? 请帮忙? Thanks!!! 谢谢!!!
Change your code to: 将您的代码更改为:
final EditText edC = (EditText) findViewById(R.id.etC);
final TextView tvD = (TextView) findViewById(R.id.tvDisplay);
tvD.setText("Enter amount of credits");
edC.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
String creditsS = edC.getText().toString().trim();
int credits = Integer.parseInt(creditsS);
if (actionId == EditorInfo.IME_ACTION_DONE) {
tvD.setText("Your amount of credits" + credits);
return true;
}
return false;
}
});
It's crashing because you're getting the value from edC before there is any value, so it's trying to parse an empty string and it crashes with NumberFormatException. 之所以崩溃,是因为您在没有任何值之前从edC中获取了该值,因此它试图解析一个空字符串,并且崩溃与NumberFormatException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.