0
當我從編輯文本框中刪除最後一個字符時,我的應用程序強行關閉。例如如果我放入456,6和5不會導致問題,但刪除4會導致錯誤。從EditText字段中刪除所有字符時強制關閉
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;
}
使用'ADB logcat',DDMS,或在Eclipse中DDMS角度來考察LogCat並查看與「強制關閉」相關的堆棧跟蹤。 – CommonsWare 2011-04-27 13:54:51