我创建了两个SurafceView,例如surface_1和surface_2
activity_main XML中存在两个相对布局。名称是relative_1和relative_2。
然后,我将surface_1添加到relative_1,并将surface_2添加到relative_2。
现在,我想将surafce_1添加到relative_2。 为此,我首先从relative_2移除了surface_2,然后将surface_1添加到relative_2。
但是在将surface_1添加到relative_2的过程中,出现了异常 指定的孩子已经有一个父母。您必须先在孩子的父母上调用removeView()。
SurafceView surface_1= new SurfaceView(context);
SurafceView surface_2= new SurfaceView(context);
RelativeLayout relative_1 = (RelativeLayout) mainView.findViewById(R.id.relative_1 );
RelativeLayout relative_2 = (RelativeLayout) mainView.findViewById(R.id.relative_2);
relative_1.addView(surface_1)
relative_2.addView(surface_2)
10秒后(在处理程序延迟发布中),我编写了以下代码。
int childCount = relative_2.getChildCount();
if (childCount > 0) {
View temp1 = relative_2.getChildAt(0);
relative_2.removeView(temp1);
}
relative_2.addView(surface_1) // This line gives exception
The specified child already has a parent.
You must call removeView() on the child's
parent first.