当从 EditText 字段中删除所有字符时强制关闭

发布于 2024-11-03 15:07:17 字数 1568 浏览 0 评论 0原文

当我从编辑文本框中删除最后一个字符时,我的应用程序被强制关闭。例如,如果我输入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-11-10 15:07:17

何兄弟
你在这里错过了其他

if(null==edMiles){
        this.showAnswer.setText("Please input miles");
        this.milesBox.setText(null);
    }
 else{

    double amiles = Double.parseDouble(edMiles);
    setMiles(amiles);
}

,它会给出数字格式异常,因为你解析空字符串

He broo
you have missed else here

if(null==edMiles){
        this.showAnswer.setText("Please input miles");
        this.milesBox.setText(null);
    }
 else{

    double amiles = Double.parseDouble(edMiles);
    setMiles(amiles);
}

here it will give number format exception because u r parsing null string

毁梦 2024-11-10 15:07:17

更改

if(null==edMiles){

if(null == edMiles || edMiles.length() == 0) {

或更好地使用 TextUtils.isEmpty(edMiles)
当您从 edittext 中删除最后一个字符时,toString 方法不会返回 null,而是返回 ""(空字符串)作为结果。

Change

if(null==edMiles){

To

if(null == edMiles || edMiles.length() == 0) {

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文