The specified child already has a parent. You must

2019-01-08 06:54发布

In my app, I have to switch between two layouts frequently. The error is happening in the layout posted below.

When my layout is called the first time, there isn't occurring any error and everything's fine. When I then call a different layout (a blank one) and afterwards call my layout a second time, it gives me the following error:

> FATAL EXCEPTION: main
>     java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My layout-code looks like this:

    tv = new TextView(getApplicationContext()); // are initialized somewhere else
    et = new EditText(getApplicationContext()); // in the code


private void ConsoleWindow(){
        runOnUiThread(new Runnable(){

     @Override
     public void run(){

        // MY LAYOUT:
        setContentView(R.layout.activity_console);
        // LINEAR LAYOUT
        LinearLayout layout=new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        // TEXTVIEW
        layout.addView(tv); //  <==========  ERROR IN THIS LINE DURING 2ND RUN
        // EDITTEXT
        et.setHint("Enter Command");
        layout.addView(et);
        }
    }
}

I know this question has been asked before, but it didn't help in my case.

12条回答
【Aperson】
2楼-- · 2019-01-08 07:33

simply pass the attachtoroot argument as false

View view = inflater.inflate(R.layout.child_layout_to_merge, parent_layout, false);
查看更多
Summer. ? 凉城
3楼-- · 2019-01-08 07:34

You can use this methode to check if a view has children or not .

public static boolean hasChildren(ViewGroup viewGroup) {
    return viewGroup.getChildCount() > 0;
 }
查看更多
4楼-- · 2019-01-08 07:37

If other solution is not working like:

View view = inflater.inflate(R.layout.child_layout_to_merge, parent_layout, false);

check for what are you returning from onCreateView of fragment is it single view or viewgroup? in my case I had viewpager on root of xml of fragment and I was returning viewpager, when i added viewgroup in layout i didnt updated that i have to return viewgroup now, not viewpager(view).

查看更多
来,给爷笑一个
5楼-- · 2019-01-08 07:38

I got this message while trying to commit a fragment using attach to root to true instead of false, like so:

return inflater.inflate(R.layout.fragment_profile, container, true)

After doing:

return inflater.inflate(R.layout.fragment_profile, container, false)

It worked.

查看更多
相关推荐>>
6楼-- · 2019-01-08 07:39

frameLayout.addView(bannerAdView); <----- if you get error on this line the do like below..

if (bannerAdView.getParent() != null)

  ((ViewGroup) bannerAdView.getParent()).removeView(bannerAdView);

frameLayout.addView(bannerAdView); <------ now added view

查看更多
我只想做你的唯一
7楼-- · 2019-01-08 07:43

In my case the problem was caused by the fact that I was inflating parent View with <merge> layout. In this case, addView() caused the crash.

View to_add = inflater.inflate(R.layout.child_layout_to_merge, parent_layout, true);
// parent_layout.addView(to_add); // THIS CAUSED THE CRASH

Removing addView() helped to solve the problem.

查看更多
登录 后发表回答