Search code examples
javaandroidandroid-tablelayoutremovechild

Android Studio AddView TableLayout, must removeChild Error?


I am trying to add AddView's to my TableLayout while limiting the amount of items on each row to 5, so if I have 3 items it will make 1 row, 6 items will load 2, 11 will load 3 etc. I am pretty new to working with Android so it may be a simple fix, please let me know. `

    final TableLayout t1 = (TableLayout) findViewById(R.id.t1); //TableLayout to expand
    final TableRow tr1 = new TableRow(this); //Row 1 0-5 items
    final TableRow tr2 = new TableRow(this); // Row 2 6-10 itmes
    final TableRow tr3 = new TableRow(this);
    final TableRow tr4 = new TableRow(this);


    final Button finalBtn = btn; //Button that triggers Action

    btn.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View view) {
            // Initialize a new ImageView widget
            if (array_Killerimage.size() <= 5)
            {
                tl.addView(tr1); //Crashes on all these lines
            }
            if (array_Killerimage.size() <= 10)
            {
                t1.addView(tr1);
                tl.addView(tr2);
            }
            if (array_Killerimage.size() <= 15)
            {
                t1.addView(tr1);
                t1.addView(tr2);
                t1.addView(tr3);
            }
            if (array_Killerimage.size() <= 20)
            {
                tl.addView(tr1);
                tl.addView(tr2);
                tl.addView(tr3);
                tl.addView(tr4);

            }

ERROR LOG

  Process: com.example.appv2, PID: 11200
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:5122)
    at android.view.ViewGroup.addView(ViewGroup.java:4953)
    at android.widget.TableLayout.addView(TableLayout.java:427)
    at android.view.ViewGroup.addView(ViewGroup.java:4893)
    at android.widget.TableLayout.addView(TableLayout.java:409)
    at android.view.ViewGroup.addView(ViewGroup.java:4866)
    at android.widget.TableLayout.addView(TableLayout.java:400)
    at com.example.appv2.Main4Activity$1.onClick(Main4Activity.java:) //Line of interest
    at android.view.View.performClick(View.java:6892)
    at android.widget.TextView.performClick(TextView.java:12693)
    at android.view.View$PerformClick.run(View.java:26101)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Solution

  • It means the View you are adding to TabelLayout already has a parent. you can simply remove the view from its parent before adding to new Container(TabelLayout)

    do this change before adding view.

    if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
       }
    tabelLayout.addView(view);
    

    Note: you can only add views which doesnt have parent or in other words you can only add views which have not been added to view tree, once its added you should remove the view from its parent and only then you can add to another parent.

    Happy coding!!