android动态生成radiobutton,android – 使用字符串数组动态创建RadioButton

我试图从字符串数组创建RadioButton,但如果我只使用字符串它不工作,那么它似乎工作正常

Log Cat显示错误“ava.lang.IllegalStateException:指定的子节点已经有父节点.您必须首先在子节点的父节点上调用removeView().”

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

请检查代码并纠正我,以便我进一步使用我的应用程序

这是代码

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

{

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

寻求帮助

谢谢

解决方法:

IllegalStateException: The specified child already has a parent. You

must call removeView() on the child’s parent first.

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

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

for(int i =0; i

{

RadioButton radioButtonView = new RadioButton(this);

radioButtonView.setText(ab[i]);

radioGroup.addView(radioButtonView, p);

((ViewGroup)layout.getParent()).removeView(layout);

}

在for循环中以避免错误.

标签:android

来源: https://codeday.me/bug/20190728/1560175.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 .NET WinForm 中,可以使用 `System.Windows.Forms.RadioButton` 类来动态生成单选按钮。 首先,你需要创建一个新的 `RadioButton` 对象,并设置它的属性,如位置、大小、文本等。 ```c# RadioButton rb = new RadioButton(); rb.Text = "RadioButton Text"; rb.Location = new Point(10, 10); rb.Size = new Size(100, 30); ``` 然后,将新创建的 `RadioButton` 对象添加到窗体的控件集合中。 ```c# this.Controls.Add(rb); ``` 如果你需要动态生成多个单选按钮, 你可以放在循环里面,来动态的创建。 ```c# for (int i = 0; i < 5; i++) { RadioButton rb = new RadioButton(); rb.Text = "RadioButton " + i; rb.Location = new Point(10, 10 + i * 30); rb.Size = new Size(100, 30); this.Controls.Add(rb); } ``` 这样就可以在界面上动态生成多个单选按钮了。 ### 回答2: 在.NET WinForms中动态生成RadioButton控件非常简单。首先,需要在窗体上添加一个Panel控件,用于容纳生成RadioButton控件。然后,可以使用以下代码来动态生成RadioButton控件并将其添加到Panel上: ```csharp // 创建一个RadioButton控件的实例 RadioButton radioButton = new RadioButton(); // 设置RadioButton的属性 radioButton.Text = "选项1"; radioButton.Location = new Point(10, 10); // 设置RadioButton在窗体中的位置 // 添加RadioButton到Panel控件中 panel1.Controls.Add(radioButton); ``` 上面的代码首先创建了一个RadioButton控件的实例,并设置了其文本和位置属性。然后,使用`panel1.Controls.Add()`方法将RadioButton控件添加到Panel控件中。可以根据需要重复使用上述代码,以动态生成更多的RadioButton控件。 当然,还可以为RadioButton控件设置事件处理程序,以响应用户的操作。例如,可以使用以下代码为动态生成RadioButton控件添加一个事件处理程序: ```csharp radioButton.CheckedChanged += new EventHandler(radioButton_CheckedChanged); void radioButton_CheckedChanged(object sender, EventArgs e) { // 在这里编写处理代码,响应RadioButton的选中状态变化 } ``` 在上述代码中,`radioButton_CheckedChanged`方法将被调用,当动态生成RadioButton控件的选中状态发生变化时。可以在该事件处理程序中编写相应的代码,以响应RadioButton的选中状态变化。 总之,通过添加Panel控件,使用代码动态生成RadioButton控件,并为其设置必要的属性和事件处理程序,可以在.NET WinForms界面上动态生成RadioButton控件。 ### 回答3: 在.NET WinForms中,我们可以使用代码动态生成RadioButton控件并将其添加到界面上。 首先,我们需要创建一个RadioButton对象,并设置其属性,例如文本(Text)和位置(Location)。例如,以下代码将创建一个文本为"选项1",位置为(10, 10)的RadioButton控件: RadioButton radioButton1 = new RadioButton(); radioButton1.Text = "选项1"; radioButton1.Location = new Point(10, 10); 接下来,我们需要将其添加到窗体的控件集合中。可以使用窗体对象的Controls属性来访问控件集合。例如,以下代码将添加上述的RadioButton控件: this.Controls.Add(radioButton1); 通过重复上述步骤,我们可以添加任意数量的RadioButton控件,并设置它们的位置和文本。例如,以下代码将添加3个不同的RadioButton控件: RadioButton radioButton1 = new RadioButton(); radioButton1.Text = "选项1"; radioButton1.Location = new Point(10, 10); this.Controls.Add(radioButton1); RadioButton radioButton2 = new RadioButton(); radioButton2.Text = "选项2"; radioButton2.Location = new Point(10, 30); this.Controls.Add(radioButton2); RadioButton radioButton3 = new RadioButton(); radioButton3.Text = "选项3"; radioButton3.Location = new Point(10, 50); this.Controls.Add(radioButton3); 通过以上步骤,我们就能够动态生成RadioButton控件并将其添加到界面上。在实际项目中,我们可以使用循环结构来简化添加多个RadioButton控件的过程,以便更高效地生成控件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值