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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuzehn/article/details/88694437

刚才在做android的view显示时,想用LinearLayout关联一个view来展示远端视频流的画面。报了如下一个错误:

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

解决方法:我首先根据错误的提示,说这当前view已经有了自己的父view,我们需要首先清除父view的其他子view。于是我写了如下代码:

ViewGroup parentViewGroup = (ViewGroup) currentView.getParent();
if (parentViewGroup != null) {
    parentViewGroup.removeAllViews();
}
linearLayout.addView(CurrentView);

这样修改以后,报错确实消失了,但是仍然没有解决我的问题,视频画面依然没有显示出来。所以,我又采取了剥离LinearLayout和View的方法,让view独立存在,并绑定holder与mediaplayer,画面就出来了。

如果还是显示不出来,可以尝试:

surfaceView.setZOrderOnTop(true);

猜你喜欢

转载自blog.csdn.net/liuzehn/article/details/88694437
今日推荐