Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:12:46+00:00 2026-05-29T15:12:46+00:00

For my android application, I need to change the textChangedListener each time a RadioButton

  • 0

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}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:12:46+00:00Added an answer on May 29, 2026 at 3:12 pm

    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

        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.text1);
            final EditText t2 = (EditText) findViewById(R.id.text2);
            final EditText t3 = (EditText) findViewById(R.id.text3);
            final EditText t4 = (EditText) findViewById(R.id.text4);
            final Context c = getApplicationContext();
    
            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) {
                    Toast.makeText(c, "onTextChanged called(_list1 watcher)", Toast.LENGTH_SHORT).show();
                }
            });
            _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) {
                    Toast.makeText(c, "onTextChanged called(_list2 watcher)", Toast.LENGTH_SHORT).show();
                }
            });
            active.addTextChangedListener(_list1);
            last = _list1;
        }
    
    }
    

    Following is the code for main.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <RadioButton
                android:id="@+id/But1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But1" />
    
            <EditText
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text" />
    
            <RadioButton
                android:id="@+id/But2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But2" />
    
            <EditText
                android:id="@+id/text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text" />
    
            <RadioButton
                android:id="@+id/But3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But3" />
    
            <EditText
                android:id="@+id/text3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text" />
    
            <RadioButton
                android:id="@+id/But4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But4" />
    
            <EditText
                android:id="@+id/text4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text" >
            </EditText>
        </RadioGroup>
    
    </LinearLayout>
    

    Try this. Its working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use map in my Android application. There are many Android platforms.
In one of my Android Application I need to keep the title bar same
I need to add a shake feature that will refresh my Android application. All
I need to run a periodic task in an Android application. I currently use
I am developing an Android application where I have to maintain session. There are
My web application target to major Smartphones and I need to change the CSS
for my android application,I need to take a photo with the built-in camera and
In my Android application I need to read a large amount of data from
I am developing an android application where i need to enter multiple rows in
I'm making an Android application where I need to slow down the audio being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.