开发者

Why cant I access the children of the radio group

I am trying to use this Android: How to get a radiogroup with togglebuttons? code of the given answer

but in

static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
            for (int 开发者_如何学编程j = 0; j < radioGroup.getChildCount(); j++) {
                final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
                view.setChecked(view.getId() == i);
            }
        }
    };

in line

final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);

it always crashes. In Logcat I don't see any message.

I tried everything I could think of , but can't fid the problem - Many thanks!

ps this is my xml for the radiogroup:

   <RadioGroup android:id="@+id/radioGroup2" android:layout_width="150sp" android:layout_height="wrap_content" 
            android:paddingLeft = "10sp" android:layout_alignBottom="@+id/a2"  >
        <RadioButton android:layout_width="wrap_content" android:id="@+id/Settings_otherSettingsT2Yes" android:layout_height="wrap_content" 
            android:textColor="#000000" android:textSize="18sp"
        android:text="@string/Settings_otherSettingsT2Yes" android:checked="false"></RadioButton>
        <RadioButton android:layout_width="wrap_content" android:id="@+id/Settings_otherSettingsT2No" android:layout_height="wrap_content" 
            android:textColor="#000000" android:textSize="18sp"
        android:text="@string/Settings_otherSettingsT2No"></RadioButton>
    </RadioGroup>

...nothing special here

I checked the number of children by logging radioGroup.getChildCount() and it gives 2 as expected


In your code you cast RadioButtons to ToggleButton. This is likely to be the cause of your crashes. I don't understand why you can't find an exception in logcat.

However, you say you want to use code like in the link, but your XML is not close to the link. You use RadioButtons where the link uses ToggleButtons.

If you just want RadioButtons, then completely disregard that link. If you want to use ToggleButtons then that link isn't the best way anyway. Adding ToggleButtons to a RadioGroup just confuses the intent of the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜