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

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

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

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


当前回答

我来到这里用我的recyclerview搜索错误,但解决方案没有工作(显然)。我已经写了原因和解决方案,以防recyclerview。希望它能帮助到别人。

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

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

相反,它应该是

return new VH(layoutInflater.inflate(R.layout.single_row, null));

其他回答

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

如果您的项目是在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,它将安全地不做任何事情

在我的情况下,我不小心从Layout.onCreateView()中返回一个子视图,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.activity_deliveries, container, false);
    RecyclerView rv  = (RecyclerView) v.findViewById(R.id.deliver_list);
    
    return rv; // <- here's the issue
}

解决方案是返回父视图(v)而不是子视图(rv)。

如果你正在使用ViewBinding,请确保你引用了正确的绑定!

当我试图从一个活动中膨胀一个自定义对话框时,我有这个问题:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

final AlertBinding alertBinding = AlertBinding.inflate(LayoutInflater.from(this), null, false);

builder.setView(binding.getRoot()); // <--- I was using binding (which is my Activity's binding), instead of alertBinding.

我的问题与许多其他答案有关,但需要做出改变的原因略有不同……我试图将一个活动转换为一个片段。所以我把膨胀代码从onCreate移动到onCreateView,但我忘记从setContentView转换到膨胀方法,同样的IllegalStateException把我带到了这个页面。

我改了这个:

binding = DataBindingUtil.setContentView(requireActivity(), R.layout.my_fragment)

:

binding = DataBindingUtil.inflate(inflater, R.layout.my_fragment, container, false)

这就解决了问题。

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

if (bannerAdView.getParent() != null)

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

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