我编写了这段代码,由于文本类型为十进制,因此当我尝试将可编辑的值转换为整数时,程序崩溃,并且由于应用程序必须将可编辑的值更改为字符串,因此我怀疑它正处于该时刻编辑文本后,编辑文本为空会使其崩溃。
EditText Y = (EditText) findViewById(R.id.y);
Y.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
if(s != null){
int Yvalue = Integer.parseInt(s.toString());
}
}
最佳答案
在我看来,在将String解析为Integer时应该发生NumberFormatException。尝试将其包含在该异常的try / catch中。
try {
Yvalue = Integer.parseInt(number);
} catch (NumberFormatException e) {
Yvalue = defaultVal; // or do whatever necessary
}
关于java - 可编辑以字符串崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22907559/