我必须经常在两种布局之间切换。错误发生在下面发布的布局中。

当我的布局第一次被调用时,没有发生任何错误,一切都很好。当我然后调用一个不同的布局(一个空白的),然后调用我的布局第二次,它抛出以下错误:

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

我的布局代码是这样的:

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

我知道以前有人问过这个问题,但它对我的情况没有帮助。


当前回答

首先必须从父视图中移除子视图。

如果您的项目是在Kotlin中,那么您的解决方案将与Java略有不同。Kotlin使用as?,如果左侧为空或强制转换失败,则返回null。

(childView.parent as? ViewGroup)?.removeView(childView)
newParent.addView(childView)

Kotlin扩展解决方案

如果需要多次执行此操作,请添加此扩展以使代码更具可读性。

childView.removeSelf()

fun View?.removeSelf() {
    this ?: return
    val parentView = parent as? ViewGroup ?: return
    parentView.removeView(this)
}

如果这个视图为空,父视图为空,或者父视图不是ViewGroup,它将安全地不做任何事情

其他回答

当我试图使用attach to root将一个片段提交到true而不是false时,我得到了这个消息,就像这样:

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

后做的事情:

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

它工作。

frameLayout.addView (bannerAdView);<-----如果你在这一行得到错误,做如下..

if (bannerAdView.getParent() != null)

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

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

就我而言,我做错了:

...
TextView content = new TextView(context);
for (Quote quote : favQuotes) {
  content.setText(quote.content);
...

代替(好):

...
for (Quote quote : favQuotes) {
  TextView content = new TextView(context);
  content.setText(quote.content);
...

我找到了另一个解决办法:

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

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

这也设置了积极和消极按钮(确定和取消按钮)与onClickListeners。

首先必须从父视图中移除子视图。

如果您的项目是在Kotlin中,那么您的解决方案将与Java略有不同。Kotlin使用as?,如果左侧为空或强制转换失败,则返回null。

(childView.parent as? ViewGroup)?.removeView(childView)
newParent.addView(childView)

Kotlin扩展解决方案

如果需要多次执行此操作,请添加此扩展以使代码更具可读性。

childView.removeSelf()

fun View?.removeSelf() {
    this ?: return
    val parentView = parent as? ViewGroup ?: return
    parentView.removeView(this)
}

如果这个视图为空,父视图为空,或者父视图不是ViewGroup,它将安全地不做任何事情