Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating RadioGroup programmatically

I'm getting an Error while working with the following code

Error: The specified child already has a parent you must call removeView on the child's parent first

Anyone please help me in resolving this Error:

RadioButton[] radiobutton = new RadioButton[5]; RadioGroup radiogroup = new RadioGroup(this); for (int i = 0; i < 5; i++) {      LinearLayout listmainLayout = (LinearLayout) findViewById(R.id.phonelist);     // listmainLayout.setLayoutParams(new     // ListView.LayoutParams(width,     // height / 10));     listmainLayout.setGravity(Gravity.CENTER_VERTICAL);     RelativeLayout mainlistLayout = new RelativeLayout(getApplicationContext());     LinearLayout.LayoutParams mainlistLayoutParams = new LinearLayout.LayoutParams(         (int) (width * 0.85), LayoutParams.WRAP_CONTENT);     mainlistLayout.setLayoutParams(mainlistLayoutParams);     // mainlistLayout.setOrientation(LinearLayout.VERTICAL);     mainlistLayout.setPadding((int) (0.03 * width), 0, 0, 0);     LinearLayout.LayoutParams radiogroupparams = new LinearLayout.LayoutParams(         LinearLayout.LayoutParams.WRAP_CONTENT,         LinearLayout.LayoutParams.WRAP_CONTENT);     radiogroup.setLayoutParams(radiogroupparams);     radiobutton[i] = new RadioButton(this);     radiobutton[i].setText(" " + ContactsActivity.phonetype.get(i)         + "    " + ContactsActivity.phone.get(i));     radiobutton[i].setId(i + 100);     radiogroup.removeView(radiobutton[i]);     radiogroup.addView(radiobutton[i]);     mainlistLayout.addView(radiogroup); } 

My logcat shows:

11-12 17:51:11.500: E/AndroidRuntime(3353): FATAL EXCEPTION: main 11-12 17:51:11.500: E/AndroidRuntime(3353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contacts_appetite/com.example.contacts_appetite.ContactDetalisList}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.ActivityThread.access$600(ActivityThread.java:130) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.os.Handler.dispatchMessage(Handler.java:99) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.os.Looper.loop(Looper.java:137) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.ActivityThread.main(ActivityThread.java:4745) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at java.lang.reflect.Method.invokeNative(Native Method) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at java.lang.reflect.Method.invoke(Method.java:511) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at dalvik.system.NativeStart.main(Native Method) 11-12 17:51:11.500: E/AndroidRuntime(3353): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3378) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.view.ViewGroup.addView(ViewGroup.java:3249) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.view.ViewGroup.addView(ViewGroup.java:3194) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.view.ViewGroup.addView(ViewGroup.java:3170) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at com.example.contacts_appetite.ContactDetalisList.getView(ContactDetalisList.java:139) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at com.example.contacts_appetite.ContactDetalisList.onCreate(ContactDetalisList.java:65) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.Activity.performCreate(Activity.java:5008) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 11-12 17:51:11.500: E/AndroidRuntime(3353):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 11-12 17:51:11.500: E/AndroidRuntime(3353):     ... 11 more 
like image 285
Sai Durga Avatar asked Nov 12 '13 12:11

Sai Durga


People also ask

How to create RadioGroup programmatically in android?

setId(i); radioGrp. addView(radioButton); } //set listener to radio button group radioGrp. setOnCheckedChangeListener(new RadioGroup. OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int checkedRadioButtonId = radioGrp.

How do I add a radio button to a radio group dynamically?

For creating dynamic RadioButton, we need to use android. view. ViewGroup. LayoutParams which configures the width and height of views and implements setOnCheckedChangeListener() method of RadioGroup class.

How do you programmatically determine whether a RadioButton is checked?

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false.

How do you check whether a RadioButton is checked or not in android?

here you go. Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected. You are already doing this.


1 Answers

this The specified child already has a parent. You must call removeView() on the child's parent first. because you are adding child( radio group ) to the parent layout multiple times.

try like this

private void createRadioButton() {     final RadioButton[] rb = new RadioButton[5];     RadioGroup rg = new RadioGroup(this); //create the RadioGroup     rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL     for(int i=0; i<5; i++){        rb[i]  = new RadioButton(this);                  rb[i].setText(" " + ContactsActivity.phonetype.get(i)             + "    " + ContactsActivity.phone.get(i));        rb[i].setId(i + 100);        rg.addView(rb[i]);     }     ll.addView(rg);//you add the whole RadioGroup to the layout  } 
like image 193
RajaReddy PolamReddy Avatar answered Sep 19 '22 16:09

RajaReddy PolamReddy