gpt4 book ai didi

android - Textwatcher 给出 Stackoverflow 错误

转载 作者:行者123 更新时间:2023-11-29 14:37:45 25 4
gpt4 key购买 nike

我正在制作一个使用 TextWatcher 的长度转换器。缩短的代码是

 v1.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
val1=Double.parseDouble(v1.getText().toString());
val2=InchToCm(val1);
val3=InchToMl(val1);
val4=InchToKm(val1);
val5=InchToM(val1);
val6=InchToY(val1);
val7=InchToFt(val1);
val8=InchToLg(val1);
val9=InchToLgn(val1);
val11=InchTomm(val1);
val12=InchToNm(val1);


v2. setText(Double.toString(val2));
v3. setText(Double.toString(val3));
v4. setText(Double.toString(val4));
v5. setText(Double.toString(val5));
v6. setText(Double.toString(val6));
v7. setText(Double.toString(val7));
v8. setText(Double.toString(val8));
v9. setText(Double.toString(val9));
v11. setText(Double.toString(val11));
v12. setText(Double.toString(val12));

}

@Override
public void afterTextChanged(Editable s) {

}
});
v2.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
val2=Double.parseDouble(v2.getText().toString());
val1=CmToInch(val2);
val3=CmToMl(val2);
val4=CmToKm(val2);
val5=CmToM(val2);
val6=CmToY(val2);
val7=CmToFt(val2);
val8=CmToLg(val2);
val9=CmToLgn(val2);
val11=CmTomm(val2);
val12=CmToNm(val2);

v1. setText(Double.toString(val1));
v3. setText(Double.toString(val3));
v4. setText(Double.toString(val4));
v5. setText(Double.toString(val5));
v6. setText(Double.toString(val6));
v7. setText(Double.toString(val7));
v8. setText(Double.toString(val8));
v9. setText(Double.toString(val9));
v11. setText(Double.toString(val11));
v12. setText(Double.toString(val12));

}

@Override
public void afterTextChanged(Editable s) {

}
});

我收到一个 stackoverflow 错误。如果我完全删除 v2 的 textwatcher 代码。v1 的文本观察器工作完美..我只希望我所有的 edittext 都能与 textwatcher 一起使用。我缺少什么?我应该改变什么?

这是日志

08-20 20:31:28.122: E/AndroidRuntime(30377): FATAL EXCEPTION: main
08-20 20:31:28.122: E/AndroidRuntime(30377): java.lang.StackOverflowError
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.text.TextUtils.getChars(TextUtils.java:77)
08-20 20:31:28.122: E/AndroidRuntime(30377): android.text.TextUtils.indexOf(TextUtils.java:110)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.text.StaticLayout.generate(StaticLayout.java:182)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.makeSingleLayout(TextView.java:6078)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.makeNewLayout(TextView.java:5976)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.checkForRelayout(TextView.java:6515)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.setText(TextView.java:3787)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.setText(TextView.java:3645)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.EditText.setText(EditText.java:80)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.setText(TextView.java:3620)
08-20 20:31:28.122: E/AndroidRuntime(30377): at com.tdx.simplelegthconverter.MainActivity$1.onTextChanged(MainActivity.java:55)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.sendOnTextChanged(TextView.java:7338)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.setText(TextView.java:3790)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.setText(TextView.java:3645)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.EditText.setText(EditText.java:80)
08-20 20:31:28.122: E/AndroidRuntime(30377): at android.widget.TextView.setText(TextView.java:3620)
08-20 20:31:28.122: E/AndroidRuntime(30377): at com.tdx.simplelegthconverter.MainActivity$2.onTextChanged(MainActivity.java:95)

最佳答案

您正在创建 v1 和 v2 之间无限循环的文本更改。

每当您在 v1 中编辑某些内容时,TextWatcher 会设置 v2 的文本,进而触发 v2 的 TextWatcheronTextChanged() .现在,v2 的 TextWatcher 设置 v1 的文本(再次触发 v1 的 onTextChanged())。这将创建一个永无止境的循环,您将收到 StackOverflow 异常。

您必须更改代码流。

编辑:我正在展示一个示例,如何消除两个 EditText 字段的 StackOverflow 错误。您可以根据需要对任意数量的 EditText 使用类似的编码。

首先,您可以像这样扩展 TextWatcher(这将帮助您保持代码的组织性。而且,您可以对所有 EditText 使用相同的 TextWatcher):

class MyInputWatcher implements TextWatcher {
private EditText et;

private MyInputWatcher(EditText editText) {
et = editText;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

switch (et.getId()) {
case R.id.editText1: {
v2.removeTextChangedListener(watcher2);
v2.setText(s);// set whatever text you want to set in editText2
v2.addTextChangedListener(watcher2);
//add the above 3 lines of code for other EditTexts, too (if any)
break;
}
case R.id.editText2: {
v1.removeTextChangedListener(watcher1);
v1.setText(s); // set whatever text you want to set in editText1
v1.addTextChangedListener(watcher1);

break;
}
}
}

@Override
public void afterTextChanged(Editable s) {

}
}

诀窍是在调用 setText() 之前删除 onTextChangeListeners。然后,在完成设置文本后将它们添加回去。

声明这些成员变量:

private EditText v1, v2;
private MyInputWatcher watcher1, watcher2;

现在,在 onCreate() 方法中,实现这个:

v1 = (EditText)findViewById(R.id.editText1);
v2 = (EditText)findViewById(R.id.editText2);

watcher1 = new MyInputWatcher(v1);
watcher2 = new MyInputWatcher(v2);

v1.addTextChangedListener(watcher1);
v2.addTextChangedListener(watcher2);

为了方便起见,整个 Activity 类如下所示:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

EditText v1, v2;
private MyInputWatcher watcher1, watcher2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v1 = (EditText)findViewById(R.id.editText1);
v2 = (EditText)findViewById(R.id.editText2);

watcher1 = new MyInputWatcher(v1);
watcher2 = new MyInputWatcher(v2);

v1.addTextChangedListener(watcher1);
v2.addTextChangedListener(watcher2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

class MyInputWatcher implements TextWatcher {
private EditText et;

private MyInputWatcher(EditText editText) {
et = editText;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

switch (et.getId()) {
case R.id.editText1: {
v2.removeTextChangedListener(watcher2);
v2.setText(s);// set whatever text you want to set in editText2
v2.addTextChangedListener(watcher2);
break;
}
case R.id.editText2: {
v1.removeTextChangedListener(watcher1);
v1.setText(s); // set whatever text you want to set in editText1
v1.addTextChangedListener(watcher1);

break;
}
}
}

@Override
public void afterTextChanged(Editable s) {

}
}
}

关于android - Textwatcher 给出 Stackoverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25407959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com