0

I am trying to add multiple views of the same layout. however, when doing the below code I am getting Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

What am i doing wrong? thanks!

class GetTrackTimer extends TimerTask {

            Activity act;
            View RootView;
            public GetTrackTimer(Activity ctx, View RootView)
            {
                this.act = ctx;
                this.RootView = RootView;
            }

            @Override
            public void run() {

                act.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {


                        if (TrackItems.getInstance().items == null)
                            return;

                        LinearLayout items = (LinearLayout) RootView.findViewById(R.id.itemslist);
                        LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        for (int i = 0; i < TrackItems.getInstance().items.length; i++)
                        {

                            View  itemView = inflater.inflate(R.layout.track_item,null);
                            TextView tv=  (TextView)itemView.findViewById(R.id.itemName);
                            tv.setText(TrackItems.getInstance().items[i].Item.ID + "");

                            items.addView(tv);
                        }
                    }
                });
            }
        }

2 Answers 2

2

Your TextView tv is already a child of the inflated itemView, so you can't add it as a child to another view.

Try items.addView(itemView); instead of items.addView(tv);

Sign up to request clarification or add additional context in comments.

Comments

1

That's because you cannot multiple parent layout for single child element..

In here, items.addView(tv); your tv is added multiple times, hence the error..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.