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条回答
欢心
2楼-- · 2019-01-08 07:44

check if you already added the view

if (textView.getParent() == null)
    layout.addView(textView);
查看更多
冷血范
3楼-- · 2019-01-08 07:45

The error message says what You should do.

// TEXTVIEW
if(tv.getParent() != null) {
    ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
layout.addView(tv); //  <==========  ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT
查看更多
Deceive 欺骗
4楼-- · 2019-01-08 07:46

The code below solved it for me:

@Override
public void onDestroyView() {
    if (getView() != null) {
        ViewGroup parent = (ViewGroup) getView().getParent();
        parent.removeAllViews();
    }
    super.onDestroyView();
}

Note: The error was from my fragment class and by overriding the onDestroy method like this, I could solve it.

查看更多
萌系小妹纸
5楼-- · 2019-01-08 07:47
if(tv!= null){
    ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-08 07:48

I found another fix:

if (mView.getParent() == null) {
                    myDialog = new Dialog(MainActivity.this);
                    myDialog.setContentView(mView);
                    createAlgorithmDialog();
                } else {
                    createAlgorithmDialog();
                }

Here i just have an if statement check if the view had a parent and if it didn't Create the new dialog, set the contentView and show the dialog in my "createAlgorithmDialog()" method.

This also sets the positive and negative buttons (ok and cancel buttons) up with onClickListeners.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-08 07:55

I came here on searching the error with my recyclerview but the solution didn't work (obviously). I have written the cause and the solution for it in case of recyclerview. Hope it helps someone.

The error is caused if in the onCreateViewHolder() the following method is followed:

layoutInflater = LayoutInflater.from(context);
return new VH(layoutInflater.inflate(R.layout.single_row, parent));

Instead it should be

return new VH(layoutInflater.inflate(R.layout.single_row, null));
查看更多
登录 后发表回答