Android RadioGroup获取RadioButton索引

时间:2014-07-23 17:02:13

标签: android android-radiogroup android-radiobutton

我以编程方式创建了一个RadioGroup并在其中添加了一些RadioButton(它们可以是1或2或3,它依赖于数据库)。 我尝试了这段代码但是当我尝试获取RadioButton选择的ID

时崩溃了
    final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroupTeamId);
    for (int i = 0; i < teamID.size(); i++)
    {
        RadioButton radioButton = new RadioButton(getActivity());
        radioButton.setText(i);
        if(i == 0)
            radioButton.setChecked(true);
        radioButton.setId(i);
        radioGroup.addView(radioButton);
    }
    radioGroup.invalidate();

    ........
    ........

    builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {
            int teamId = radioGroup..getCheckedRadioButtonId(); // Here the app crashes

            .........
        }
    });

XML文件:

    <RadioGroup
        android:id="@+id/radioGroupTeamId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </RadioGroup>

日志:

    07-23 18:49:40.920: E/AndroidRuntime(5437): FATAL EXCEPTION: main
    07-23 18:49:40.920: E/AndroidRuntime(5437): android.content.res.Resources$NotFoundException: String resource ID #0x0
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.content.res.Resources.getText(Resources.java:247)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.TextView.setText(TextView.java:3473)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.example.apptesina.FragmentFindOperations.selectedItem(FragmentFindOperations.java:185)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.example.apptesina.FragmentFindOperations.access$1(FragmentFindOperations.java:154)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.example.apptesina.FragmentFindOperations$1.onItemClick(FragmentFindOperations.java:148)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.os.Handler.handleCallback(Handler.java:605)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.os.Looper.loop(Looper.java:137)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

这是因为您正在通过代码添加单选按钮,getCheckedRadioButtonId会查找单选按钮资源ID,这可以在xml布局中添加单选按钮时完成。

请参阅this

 <RadioGroup
    android:id="@+id/radioGroupTeamId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male" 
        android:checked="true" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>

或查看this帖子

您可以手动跟踪当前所选项目,也可以在每个单选按钮上创建单击侦听器。