0

I added a radio button group to a layout in my project, and on the fragment of this layout I did on this a listener (I use binding). "radioDen" is my radio button group name:

mBinding.radioDen.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch(checkedId) {
                    case R.id.radio_den_circle:
                            mBinding.setDiameterVisibility(true);
                            mBinding.setHeightVisibility(false);
                            mBinding.setWidthVisibility(false);
                        break;
                    case R.id.radio_den_none:
                            mBinding.setDiameterVisibility(false);
                            mBinding.setHeightVisibility(false);
                            mBinding.setWidthVisibility(false);
                        break;
                    case R.id.radio_den_rectangle:
                            mBinding.setDiameterVisibility(false);
                            mBinding.setHeightVisibility(true);
                            mBinding.setWidthVisibility(true);
                        break;                    
                }
            }
        }); 
    

But when I choose one of the radio buttons -the project fall, even though that it came to the listener to the right place. When I looked in radio button -android developers , I see that I have to call in the activity to

public void onRadioButtonClicked(View view) {
        } 

So I just added it like this without any code inside and now it doesn't fall any more, and it keep coming like before to the listener in the fragment.

What is the reason to this problem? Is it a bug of Android? Do you know about another solution? It is just ridiculous to leave it like this...

I checked it now I removed from main activity the function and I do get a fatal error:

FATAL EXCEPTION: main

java.lang.IllegalStateException: Could not find method onRadioButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'radio_den_rectangle'"

Community
  • 1
  • 1
batsheva
  • 2,175
  • 1
  • 20
  • 32
  • 1
    Show some more of your code and logs – yashkal Dec 07 '17 at 13:09
  • @Lakshay I just updated my question – batsheva Dec 07 '17 at 13:27
  • Please go through to these two link hope it might help you [link1](https://stackoverflow.com/questions/23973613/android-radio-group-oncheckchangedlistener-crashing-app) and [link2](https://stackoverflow.com/questions/18733326/nosuchmethodexception-with-a-radio-button) – yashkal Dec 07 '17 at 13:34
  • Could you add the XML layout files for the relevant elements (specifically the place where you define the radio buttons and radio button group)? – Jules Dupont Dec 07 '17 at 18:44

1 Answers1

0

@Lakshay you helped me find the answer, the problem was that in XML I had OnClick reference:

<RadioButton android:id="@+id/radio_den_circle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/den_circle"
                    android:onClick="onRadioButtonClicked"
                    android:padding="5dp"
                    android:layout_marginEnd="10dp"
                    android:textSize="18dp"
                    android:buttonTint="?attr/colorAccent"/>

I just removed it and it works as well !

thanks a lot !!

batsheva
  • 2,175
  • 1
  • 20
  • 32