当从 EditText 字段中删除所有字符时强制关闭
当我从编辑文本框中删除最后一个字符时,我的应用程序被强制关闭。例如,如果我输入 456、6 和 5 不会出现问题,但删除 4 会导致错误。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
milesBox = (EditText)findViewById(R.id.enter_miles);
try{
milesBox.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
addMiles();
}
});
}catch (Exception e){
this.showAnswer.setText("TextWatcher error");
this.milesBox.setText("");
}
}// end setupviews()
public void addMiles(){
try{
String edMiles = this.milesBox.getText().toString();
if(null==edMiles){
this.showAnswer.setText("Please input miles");
this.milesBox.setText(null);
}
double amiles = Double.parseDouble(edMiles);
setMiles(amiles);
}
catch (Exception e){
this.showAnswer.setText("Please input miles in numbers");
this.milesBox.setText(null);
addMiles();
//TODO check if this line causes errors
}
}
public double getMiles() {
return miles;
}
public void setMiles(double miles) {
this.miles=miles;
}
My app is force closing when I delete the last of my characters from the edit text box. e.g. if I put in 456, 6 and 5 cause no problems but deleting 4 leads to the error.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
milesBox = (EditText)findViewById(R.id.enter_miles);
try{
milesBox.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
addMiles();
}
});
}catch (Exception e){
this.showAnswer.setText("TextWatcher error");
this.milesBox.setText("");
}
}// end setupviews()
public void addMiles(){
try{
String edMiles = this.milesBox.getText().toString();
if(null==edMiles){
this.showAnswer.setText("Please input miles");
this.milesBox.setText(null);
}
double amiles = Double.parseDouble(edMiles);
setMiles(amiles);
}
catch (Exception e){
this.showAnswer.setText("Please input miles in numbers");
this.milesBox.setText(null);
addMiles();
//TODO check if this line causes errors
}
}
public double getMiles() {
return miles;
}
public void setMiles(double miles) {
this.miles=miles;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
何兄弟
你在这里错过了其他
,它会给出数字格式异常,因为你解析空字符串
He broo
you have missed else here
here it will give number format exception because u r parsing null string
更改
为
或更好地使用
TextUtils.isEmpty(edMiles)
。当您从 edittext 中删除最后一个字符时,
toString
方法不会返回 null,而是返回""
(空字符串)作为结果。Change
To
or better use
TextUtils.isEmpty(edMiles)
.When you remove last char from edittext the
toString
method doesn't return null, it returns""
(empty string) as a result.