我以编程方式创建了一个 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)