我正在尝试将settext用于edittext。我有这个代码,因为我需要在多个edittext中使用afterTextChanged()。如果我在一个案例中使用了一个setText:应用程序崩溃但它在一个按钮的onClick外部正常工作。
public Button btn;
public EditText et1, et2, et3;
private int whoHasFocus = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testing2, container, false);
super.onCreate(savedInstanceState);
et1 = (EditText) view.findViewById(R.id.ladoA);
et1.setOnFocusChangeListener(this);
et1.addTextChangedListener(this);
et2 = (EditText) view.findViewById(R.id.ladoB);
et2.setOnFocusChangeListener(this);
et2.addTextChangedListener(this);
et3 = (EditText) view.findViewById(R.id.ladoC);
et3.setOnFocusChangeListener(this);
et3.addTextChangedListener(this);
btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
et1.setText("Works correctly.");
}
});
return view;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
switch (whoHasFocus) {
case 1:
et2.setText("It does not work.");
break;
case 2:
break;
case 3:
break;
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.ladoA:
whoHasFocus = 1;
break;
case R.id.ladoB:
whoHasFocus = 2;
break;
case R.id.ladoC:
whoHasFocus = 3;
break;
}
}
}
这是日志:
10-01 11:47:19.202 4141-4141/example.test.com.testApp D/OpenGLRenderer﹕ Enabling debug mode 0
10-01 11:47:21.855 4141-4141/example.test.com.testApp D/dalvikvm﹕ GC_FOR_ALLOC freed 234K, 35% free 11382K/17256K, paused 26ms, total 26ms
10-01 11:47:21.885 4141-4141/example.test.com.testApp I/dalvikvm-heap﹕ Grow heap (frag case) to 20.760MB for 6998416-byte allocation
10-01 11:47:21.935 4141-4143/example.test.com.testApp D/dalvikvm﹕ GC_CONCURRENT freed 8K, 25% free 18209K/24092K, paused 2ms+2ms, total 40ms
10-01 11:47:21.935 4141-4141/example.test.com.testApp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
10-01 11:47:21.935 4141-4141/example.test.com.testApp W/dalvikvm﹕ VFY: unable to resolve virtual method 371: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
10-01 11:47:21.935 4141-4141/example.test.com.testApp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-01 11:47:21.935 4141-4141/example.test.com.testApp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
10-01 11:47:21.935 4141-4141/example.test.com.testApp W/dalvikvm﹕ VFY: unable to resolve virtual method 373: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
10-01 11:47:21.935 4141-4141/example.test.com.testApp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ threadid=1: stack overflow on call to Landroid/view/View;.invalidate:VZ
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ method requires 40+20+20=80 bytes, fp is 0x57102338 (56 left)
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ expanding stack end (0x57102300 to 0x57102000)
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ Shrank stack (to 0x57102300, curFrame is 0x57107d20)
10-01 11:47:28.252 4141-4141/example.test.com.testApp D/AndroidRuntime﹕ Shutting down VM
10-01 11:47:28.252 4141-4141/example.test.com.testApp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40e37ac8)
10-01 11:47:28.342 4141-4141/example.test.com.testApp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.StackOverflowError
at android.view.View.invalidate(View.java:10849)
at android.widget.TextView.invalidateRegion(TextView.java:4975)
at android.widget.TextView.invalidateCursor(TextView.java:4966)
at android.widget.TextView.spanChange(TextView.java:8060)
at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:10023)
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
at android.text.Selection.setSelection(Selection.java:116)
at android.text.Selection.setSelection(Selection.java:127)
at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
at android.widget.TextView.setText(TextView.java:4092)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7982)
at android.widget.TextView.setText(TextView.java:4111)
at android.widget.TextView.setText(TextView.java:3924)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:3899)
at example.test.com.testApp
答案 0 :(得分:1)
您可以在afterTextChanged中设置EditText的文本,该文本再次调用afterTextChanged。这种递归调用会产生一个无限循环,导致堆栈溢出。
答案 1 :(得分:0)
在OnTextChanged中调用SetText导致无限循环.. 您可以设置一个条件来阻止它:
if (et2.getText.equals(originalValue )) {
// do nothing
} else {
et2.setText("xxx");
originalValue = et2.getText();
}
答案 2 :(得分:0)
您正在afterTextChanged
中设置文本,这会导致再次调用相同的方法,从而导致无限循环。删除侦听器,设置文本,然后重新添加侦听器。
et2.removeTextChangedListener(this);
et2.setText("this will work");
et2.addTextChangedListener(this);