Java 指定的孩子已经有一个父母。您必须首先在孩子的父母(Android)上调用 removeView()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28071349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:34:24  来源:igfitidea点击:

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

javaandroidandroid-edittexttextviewparent-child

提问by blackst0ne

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.

我知道以前有人问过这个问题,但对我来说没有帮助。

采纳答案by Zielony

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

回答by suku

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.

我来到这里是为了用我的 recyclerview 搜索错误,但解决方案没有用(显然)。在 recyclerview 的情况下,我已经写了原因和解决方案。希望它可以帮助某人。

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

如果onCreateViewHolder()遵循以下方法,则会导致错误:

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));

回答by Joshua Orotayo

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.

在这里,我只有一个 if 语句,检查视图是否有父级,如果没有创建新对话框,设置 contentView 并在我的“createAlgorithmDialog()”方法中显示对话框。

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

这也设置了 onClickListeners 的正面和负面按钮(确定和取消按钮)。

回答by soshial

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.

在我的情况下,问题是由于我使用<merge>布局膨胀父视图这一事实引起的。在这种情况下,addView()导致了崩溃。

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.

删除addView()有助于解决问题。

回答by Pb Studies

simply pass the attachtoroot argument as false

只需将 attachtoroot 参数传递为 false

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

回答by Sofiane Majdoub

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

您可以使用此方法来检查视图是否有子视图。

public static boolean hasChildren(ViewGroup viewGroup) {
    return viewGroup.getChildCount() > 0;
 }

回答by blackHawk

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).

检查您从片段的 onCreateView 返回的内容是单个视图还是视图组?在我的情况下,我在片段 xml 的根目录上有 viewpager,我正在返回 viewpager,当我在布局中添加 viewgroup 时,我没有更新我现在必须返回 viewgroup,而不是 viewpager(view)。

回答by Hudson Pereira

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

我在尝试使用 attach to root 为 true 而不是 false 提交片段时收到此消息,如下所示:

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

After doing:

做完之后:

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

It worked.

有效。

回答by Dan Alboteanu

check if you already added the view

检查您是否已经添加了视图

if (textView.getParent() == null)
    layout.addView(textView);

回答by AK 12

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.

注意:错误来自我的片段类,通过像这样覆盖 onDestroy 方法,我可以解决它。