addTextChangedListener抛出StackOverflow错误

时间:2016-06-01 07:37:37

标签: android

我有这段代码:

areasqm.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        double val = Double.parseDouble(areasqm.getText().toString());
        areaha.setText(String.valueOf(val/10000));
    }      
});

areaha.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        double val = Double.parseDouble(areaha.getText().toString());
        areasqm.setText(String.valueOf(val*10000));
    }      
});

每当我在其中一个EditTexts中输入一个值时,我的应用程序就会崩溃并抛出 java.lang.StackOverflowError

为什么呢?如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

原因是:当您致电areaha.setText(String.valueOf(val/10000));时, areaha TextChangedListener 会被触发并调用areasqm.setText(String.valueOf(val*10000));,因此 TextChangedListener < / strong> of areasqm 也会被触发并调用areaha.setText(String.valueOf(val/10000));。所以你有永恒的循环,因此这个 java.lang.StackOverflowError。

你可以尝试这样的事情。

private TextWatcher areasmlWatcher = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        double val = Double.parseDouble(areasqm.getText().toString());
        areaha.removeTextChangedListener(areahaWatcher);
        areaha.setText(String.valueOf(val / 10000));
        areaha.addTextChangedListener(areahaWatcher);
    }
};

private TextWatcher areahaWatcher = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        double val = Double.parseDouble(areaha.getText().toString());
        areasqm.removeTextChangedListener(areasmlWatcher);
        areasqm.setText(String.valueOf(val*10000));
        areasqm.addTextChangedListener(areasmlWatcher);
    }
};

private void someMethod() {
    areasqm.addTextChangedListener(areasmlWatcher);
    areaha.addTextChangedListener(areahaWatcher);
}

答案 1 :(得分:1)

你有这个错误,因为根据你的代码它是无限循环编辑文本连续更新彼此的值

答案 2 :(得分:1)

如上所述, java.lang.StackOverflowError 是由于您更改编辑文本时onTextChanged()setText()的递归调用。

由于您尝试将输入的值乘以1000,我能想到的是:

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    double val = Double.parseDouble(areaha.getText().toString());
    if (val % 10000 != 0) {
        val = val * 10000;
        areasqm.setText(String.valueOf(val));
    }
}

但请记住,这是条件,你的value_entered是int,而不是double或float(十进制)。

希望这会有所帮助;)