Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to replace a textChangedListener in an activity
    text
    copied!<p>For my android application, I need to change the <code>textChangedListener</code> each time a <code>RadioButton</code> switches states. There are 4 possible buttons, and a corresponding <code>EditText</code> box for each. I currently only have 2 of the 4 buttons implemented, and when I switch from an implemented button (i.e. button 1) to an unimplemented button, there is no problem. However, when I try to select another implemented button, (i.e. button 2) the application crashes.</p> <p>Bellow is some code of mine showing the implementation of my <code>textChangedListener</code>s.</p> <pre><code>public TextWatcher last; public TextWatcher _list1; public TextWatcher _list2; public EditText active; public RadioButton checked; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText t1 = (EditText) findViewById(R.id.1text); final EditText t2 = (EditText) findViewById(R.id.2text); final EditText t3 = (EditText) findViewById(R.id.3text); final EditText t4 = (EditText) findViewById(R.id.4text); final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1); g1.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId()); if (((String) checked.getText()).equalsIgnoreCase("But1")) { active.removeTextChangedListener(last); active = t1; active.setText(""); active.addTextChangedListener(_list1); last = _list1; } else if (((String) checked.getText()).equalsIgnoreCase("But2")) { active.removeTextChangedListener(last); active = t2; active.setText(""); active.addTextChangedListener(_list2); last = _list2; } else if (((String) checked.getText()).equalsIgnoreCase("But3")) { active = t3; } else if (((String) checked.getText()).equalsIgnoreCase("But4")) { active = t4; } } }); checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId()); active = t1; active.setText(""); _list1 = (new TextWatcher(){ public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after){} public void onTextChanged(CharSequence s, int start, int before, int count) { %Useful stuff } }); _list2 = (new TextWatcher(){ public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after){} public void onTextChanged(CharSequence s, int start, int before, int count) { %Different useful stuff } }); active.addTextChangedListener(_list1); last = _list1; } </code></pre> <p>Again, my problem is that when I try to switch from one <code>TextChangedListener</code> to another, my application crashes regardless of what is implemented within each listener. I feel like there is something very obvious which I am overlooking for the characteristics and implementation of the <code>TextChangeListener</code>s. Any help would be much appreciated!</p> <p>Thanks!</p> <p>My Error Log: (Note: the activity name is "Number")</p> <pre><code>01-31 08:29:17.673: INFO/ActivityManager(59): Displayed activity com.ikiar.engtools/.Number: 659 ms (total 659 ms) 01-31 08:29:24.083: DEBUG/AndroidRuntime(276): Shutting down VM 01-31 08:29:24.083: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main 01-31 08:29:24.103: ERROR/AndroidRuntime(276): java.lang.ClassCastException: android.text.SpannableStringBuilder 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.ikiar.engtools.Number$3.onTextChanged(Number.java:122) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.setText(TextView.java:2691) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.setText(TextView.java:2556) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.EditText.setText(EditText.java:75) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.setText(TextView.java:2531) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.ikiar.engtools.Number$1.onCheckedChanged(Number.java:71) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioGroup.access$600(RadioGroup.java:52) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.CompoundButton.setChecked(CompoundButton.java:127) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.CompoundButton.toggle(CompoundButton.java:86) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioButton.toggle(RadioButton.java:69) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.CompoundButton.performClick(CompoundButton.java:98) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.view.View$PerformClick.run(View.java:8816) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.os.Handler.handleCallback(Handler.java:587) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:92) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method) 01-31 08:29:24.113: WARN/ActivityManager(59): Force finishing activity com.ikiar.engtools/.Number 01-31 08:29:24.523: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3659b0:0x365a6c] in 365498 ns 01-31 08:29:24.653: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450232b0 com.ikiar.engtools/.Number} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload