我有這樣的代碼:addTextChangedListener拋出的StackOverflow錯誤
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的。
爲什麼?如何解決這個問題?
第一校驗值是空或不是,如果(s.length()== 0){//你的代碼} –
嘗試設置在afterTextChanged(值),而不是onTextChanged() – Mehta
你正在做一個遞歸調用。 (textChanged - > setText - > textChanged ...) – Jiyeh