2

Hello!
I'm trying to create dynamic TextViews inside a LinearLayout within my main activity. The program is (supposed to be) pushing out TextView's from resultrow XML to activity_fivethreeone XML as required. The line parentLayout.addView(textView); is throwing this error;

The specified child already has a parent. You must call removeView() on the child's parent first.

I've tried answers from similar questions but getting no wins.
Fragments - The specified child already has a parent. You must call removeView() on the child's parent first
Call removeView() on the child's parent first


class:

try {
    LinearLayout parentLayout = (LinearLayout)findViewById(R.id.linLayout);
    LayoutInflater layoutInflater = getLayoutInflater();
    View view;
    for(int counter=0;counter<=theWeeksList.size();counter++){
        view = layoutInflater.inflate(R.layout.resultrow, parentLayout, false);
        TextView textView = (TextView)view.findViewById(R.id.resultRow);
        textView.setText(theWeeksList.get(counter));
        parentLayout.addView(textView);
    }
}

I was trying to use removeView() but couldn't get it to stick.

Any help will be much appreciated!
Thanks!

Community
  • 1
  • 1
jackEarlyDays
  • 207
  • 1
  • 2
  • 7

2 Answers2

1

textView has already as parent view, as matter of fact you are able to look for it with findViewById successfully. So this line:

parentLayout.addView(textView);

is causing the exception. You probably want to add view to parentLayout

parentLayout.addView(view);

since it has been just created, it has no parent and can be added as child

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

You're trying to add an EditText that already belongs to an existing ViewGroup.

Remove the line

parentLayout.addView(textView);

from your code. You don't need to do that. Replace it with

parentLayout.addView(view);
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Much appreciated! I love/hate when it's something so small! – jackEarlyDays Mar 20 '15 at 13:51
  • @jackEarlyDays: hey, sorry for replying so late ... no problem, we are all here to help each other. By the way, I answered first. I think its only fair that my answer should be marked correct :) – Yash Sampat Apr 25 '15 at 11:39