在Android中将单选按钮添加到RadioGroup时强制转换异常

在Android中将单选按钮添加到RadioGroup时强制转换异常,android,Android,我需要动态地将单选按钮添加到应用程序中,当我将按钮或组添加到布局中时,它可以完美地工作。但是,当我尝试将RadioButton添加到RadioGroup时,应用程序崩溃,出现强制转换异常:android.widget.RadioButton无法强制转换为android.widget.LinearLayout 我用这篇文章作为参考: . 这一定是一些非常基本的东西,但我已经玩了一段时间了,所以试图解决它有点不知所措 这是我的密码: protected void addRadioButtons(Li

我需要动态地将单选按钮添加到应用程序中,当我将按钮或组添加到布局中时,它可以完美地工作。但是,当我尝试将RadioButton添加到RadioGroup时,应用程序崩溃,出现强制转换异常:android.widget.RadioButton无法强制转换为android.widget.LinearLayout

我用这篇文章作为参考: . 这一定是一些非常基本的东西,但我已经玩了一段时间了,所以试图解决它有点不知所措

这是我的密码:

protected void addRadioButtons(LinearLayout layout, String switchOptions){  
    String[] optionsArray = switchOptions.split(",");
    int numberOfOptions = optionsArray.length;
    RadioButton[] radioButton = new RadioButton[numberOfOptions];
    RadioGroup radioGroup = new RadioGroup(this);
    radioGroup.setOrientation(RadioGroup.HORIZONTAL);
    LayoutParams radioGroupParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    radioGroup.setLayoutParams(radioGroupParams);
    //layout.addView(radioGroup);

    for (int i = 0; i<numberOfOptions;i++){
        radioButton[i] = new RadioButton(this);
        radioButton[i].setText(i + "");
        radioGroup.addView(radioButton[i]);
    }
    layout.addView(radioGroup);
   }
受保护的void addRadioButtons(线性布局、字符串切换选项){
字符串[]optionsArray=switchOptions.split(“,”);
int numberOfOptions=options数组.length;
RadioButton[]RadioButton=新的RadioButton[numberOfOptions];
射线组射线组=新射线组(此);
放射组设置方向(放射组水平);
LayoutParams radioGroupParams=新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.WRAP_内容);
radioGroup.setLayoutParams(radioGroupParams);
//布局。添加视图(放射组);

对于(int i=0;该异常指向此行:
radioGroup.addView(radioButton[i]);
radioGroup.addView(radioButton[i],i,radioGroup参数)
try this它不起作用。异常指向方法调用,但当我注释这一行或用布局替换radioGroup时,一切都起作用。除了所有单选按钮都可以选择之外。当你尝试注释中提到的上述方法时,会发生什么?这没有意义。我有非常类似的代码,它可以工作很好。我查找了
视图组
添加视图(视图)
,它确实希望看到
视图
,而不是
线性布局
。您最近清理过项目吗?