Using @OnCheckedChanged (ButterKnife) with radioGroup gives error in android

20,124

Solution 1

According the specification, this annotation needs to be used with 2 parameters, a CompoundButton and a boolean, so if you really want to use this listener, you have to change it like this:

@OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(CompoundButton button, boolean checked) {
   //do your stuff.
}

I think in your case this listener doesn't work, so you can use another implementation like:

@OnClick({R.id.radio_1, R.id.radio_2}) 
public void onRadioButtonClicked(RadioButton radioButton) {
    // Is the button now checked?
    boolean checked = radioButton.isChecked();

    // Check which radio button was clicked
    switch (radioButton.getId()) {
      case R.id.radio_1:
        if (checked) {
          // 1 clicked
        }
        break;
      case R.id.radio_2:
        if (checked) {
          // 2 clicked
        }
        break;
    }
}

Solution 2

this worked for me

@OnCheckedChanged({R.id.radio_button1, R.id.radio_button2})
public void onRadioButtonCheckChanged(CompoundButton button, boolean checked) {
        if(checked) {
            switch (button.getId()) {
                case R.id.radio_button1:
                    // do stuff
                    break;
                case R.id.radio_button2:
                    // do stuff
                    break;
            }
        }
    }
Share:
20,124

Related videos on Youtube

Niki
Author by

Niki

Updated on July 09, 2022

Comments

  • Niki
    Niki almost 2 years

    i recently integrated butterknife in my android project, and now i am trying to use @OnCheckedChanged annotation for radiogroup. but getting error of not giving callback. So what is the right method to call and get checkedId or this one is for radiobutton only and not for radiogroup.

    @OnCheckedChanged(R.id.gendergroupid)
    void onGenderSelected(RadioGroup group, int checkedId){
        switch(checkedId){
            case R.id.maleid:
                maleid.setEnabled(true);
                maleid.setChecked(true);
                break;
            case R.id.femaleid:
                femaleid.setEnabled(true);
                femaleid.setChecked(true);
                break;
            case R.id.bothid:
                bothid.setEnabled(true);
                bothid.setChecked(true);
                break;
        }
    }
    

    Gives me error

    BloError:(89, 10) error: Unable to match @OnCheckedChanged method arguments.

    Parameter #1: android.widget.RadioGroup did not match any listener parameters

    Parameter #2: int did not match any listener parameters

    Methods may have up to 2 parameter(s):

    android.widget.CompoundButton boolean

    These may be listed in any order but will be searched for from top to bottom.ckquote

  • Seph Remotigue
    Seph Remotigue almost 7 years
    @vuhung3990 you may have to remove the onClick implementation so it will trigger only once
  • vuhung3990
    vuhung3990 almost 7 years
    hi @Seph Remotigue, this is my mistake if radiogroup have 2 radiobutton, it will trigger 1 for notify button 1 check, 1 for notify button 2 uncheck but you have a conditional checked will only trigger 1 time, you are right
  • Someone Somewhere
    Someone Somewhere over 6 years
    Trying to use the first approach: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.CompoundButton
  • Luiz Fernando Salvaterra
    Luiz Fernando Salvaterra over 6 years
    A CompoundButton is a single button, not a group like RadioGroup.
  • Z3R0
    Z3R0 over 4 years
    RadioGroup extends LinearLayout, if you use @OnCheckedChange butterknife tryes to cast "RadioGroup" to "CompoundButton". This works only for "RadioButton" so you need to use a list of the radio buttons ids inside the radio group instead of the radio group itself.