For my android application, I need to change the textChangedListener each time a RadioButton switches states. There are 4 possible buttons, and a corresponding EditText 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.
Bellow is some code of mine showing the implementation of my textChangedListeners.
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;
}
Again, my problem is that when I try to switch from one TextChangedListener 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 TextChangeListeners. Any help would be much appreciated!
Thanks!
My Error Log: (Note: the activity name is “Number”)
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}
I think there is some problem in your layout file.
I have edited the code and created main.xml and changed EditText ids from 1text,2text,3text,4text to text1,text2,text3,text4
The code given bellow is working.
Following is the code for activity
Following is the code for main.xml file
Try this. Its working.