大佬教程收集整理的这篇文章主要介绍了android – 使用字符串数组动态创建RadioButton,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从字符串数组创建RadioButton,但如果我只使用字符串它不工作,那么它似乎工作正常
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, @R_90_10586@l 113ms
寻求帮助
谢谢
解决方法:
因为您在LinearLayout中再次添加相同的RadioButton实例.
在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循环中以避免错误.
以上是大佬教程为你收集整理的android – 使用字符串数组动态创建RadioButton全部内容,希望文章能够帮你解决android – 使用字符串数组动态创建RadioButton所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。