使用字符串数组动态创建单选按钮

时间:2015-01-08 09:07:23

标签: android

我试图从String Array创建RadioButton但是如果我只使用String它就无法工作那么它似乎工作正常 Log Cat显示错误“ava.lang.IllegalStateException:指定的子项已经有父项。您必须首先在子项的父项上调用removeView()。”

我知道错误即将来临,因为RadioButton对象没有动态创建,因此显示错误

我从这里获取代码以供参考,请检查此网址 http://android.okhelp.cz/create-radiobutton-radiogroup-dynamically-android-sample/

请检查代码并纠正我,以便我继续使用我的应用程序 这是代码

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
RadioGroup radioGroup = new RadioGroup(this);

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );

        // adding Radio Group
        layout.addView(radioGroup, p);

        // creating Radio buttons Object//
        RadioButton radioButtonView = new RadioButton(this);
        String[] ab ={"1","2"}; 

        for(int i =0; i<ab.length;i++)
        {
            radioButtonView.setText(ab[i]);
            radioGroup.addView(radioButtonView, p);
            ((ViewGroup)layout.getParent()).removeView(layout);
        }

和Log Cat错误

 I/Choreographer(4431): Skipped 547 frames!  The application may be doing too much work on its main thread.
 D/AndroidRuntime(4431): Shutting down VM
 W/dalvikvm(4431): threadid=1: thread exiting with uncaught exception (group=0xb2d2fb20)
  FATAL EXCEPTION: main
  Process: com.example.radiobuttondynamic, PID: 4431
  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
    at android.view.ViewGroup.addView(ViewGroup.java:3415)
    at android.widget.RadioGroup.addView(RadioGroup.java:141)
    at android.view.ViewGroup.addView(ViewGroup.java:3391)
    at com.example.radiobuttondynamic.Radio_Button.onCreateOptionsMenu(Radio_Button.java:46)
    at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:543)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
 D/dalvikvm(4431): GC_FOR_ALLOC freed 142K, 7% free 3094K/3308K, paused 107ms, total 113ms

寻求帮助 谢谢

4 个答案:

答案 0 :(得分:4)

如果你有这个数组: -

String route[] = {"1", "3", "4"};

您可以使用循环创建单选按钮: -

for (int i = 0; i < route.length; i++) 
{
    RadioButton radioButton = new RadioButton(this);
    radioButton.setText("Route " + String.valueOf(route[i]));
    radioButton.setId(i);
    rprms = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
    rgp.addView(radioButton, rprms);
}

您可以使用以下方式获取所选字段: - (我有SAVE按钮,所以这段代码写在onClick监听器上) 否则你也可以使用onCheckedChangeListener。

int selection = rgp.getCheckedRadioButtonId();
for (int i = 0; i < route.length; i++) 
{
    if (i == selection)
        showToast("" + route[i]);
}

答案 1 :(得分:3)

  

IllegalStateException:指定的子级已有父级。您   必须先在孩子的父母身上调用removeView()。

因为您要在RadioButton中再次添加相同的LinearLayout实例。

在for循环中创建RadioButton的对象:

   for(int i =0; i<ab.length;i++)
    {
        RadioButton radioButtonView = new RadioButton(this);
        radioButtonView.setText(ab[i]);
        radioGroup.addView(radioButtonView, p);
        ((ViewGroup)layout.getParent()).removeView(layout);
    }   

在for循环中以避免错误。

答案 2 :(得分:1)

错误是您尝试在同一布局中多次添加相同的RadioButton,您必须在for循环内移动Radiobutton创建

    String[] ab ={"1","2"}; 

    for(int i =0; i<ab.length;i++)
    {
        RadioButton radioButtonView = new RadioButton(this);
        radioButtonView.setText(ab[i]);
        radioGroup.addView(radioButtonView, p);
        ((ViewGroup)layout.getParent()).removeView(layout);
    }

答案 3 :(得分:0)

这似乎很适合我:

 LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
  for (int k = 1; k <= 20; k++) {
   //create text button
   TextView title = new TextView(this);
   title.setText("Question Number:" + k);
   title.setTextColor(Color.BLUE);
   mLinearLayout.addView(title);
   // create radio button
   final RadioButton[] rb = new RadioButton[5];
   RadioGroup rg = new RadioGroup(this);
   rg.setOrientation(RadioGroup.VERTICAL);
   for (int i = 0; i < 5; i++) {
    rb[i] = new RadioButton(this);
    rg.addView(rb[i]);
    rb[i].setText(countryName[i]);
 
   }
   mLinearLayout.addView(rg);
  }


Read more: http://www.androidhub4you.com/2013/04/dynamic-radio-button-demo-in-android.html#ixzz5dEVNFDe9

在此处查看完整代码http://www.androidhub4you.com/2013/04/dynamic-radio-button-demo-in-android.html