1

我收到"java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first android"以下代码的错误。

从日志中可以看出该行中出现了错误。setContentView(mWebPageParentView)public void onHideCustomView()API 中。

请帮助我尽早解决这个问题。

//初始声明。

private WebChromeClient mWebChromeClient = null;
private View mFullScreenVideoView;
private RelativeLayout mWebPageParentView;
private FrameLayout mFullScreenViewContainer = null;
private WebChromeClient.CustomViewCallback mFullScreenVideoViewCallback;

// 代码

@Override
public void onShowCustomView(View view, CustomViewCallback  callback) {     

if (mFullScreenVideoView != null) {
callback.onCustomViewHidden();
return;
}

// The below line was missing while writing here. 
mWebPageParentView = (RelativeLayout) findViewById(R.id.lyt_main);
// The above line was missing while writing here. 
mWebPageParentView.setVisibility(View.GONE);                        
mFullScreenViewContainer = new FrameLayout(BrowserActivity.this);
mFullScreenViewContainer.setLayoutParams(LayoutParameters);
view.setLayoutParams(LayoutParameters);
mFullScreenViewContainer.addView(view);
mFullScreenVideoView = view;
mFullScreenVideoViewCallback = callback;
mFullScreenViewContainer.setVisibility(View.VISIBLE);
setContentView(mFullScreenViewContainer);
}

@Override
public void onHideCustomView() {
 if (mFullScreenVideoView == null) {
return;
  } else {
   // Hide the custom view.  
mFullScreenVideoView.setVisibility(View.GONE);
    mFullScreenViewContainer.removeView(mFullScreenVideoView);
mFullScreenViewContainer.setVisibility(View.GONE);

mFullScreenViewContainer = null;
mFullScreenVideoViewCallback.onCustomViewHidden();
// Show the content view.  
    mWebPageParentView.setVisibility(View.VISIBLE);
    setContentView(mWebPageParentView);
    }
 }
4

1 回答 1

0

首先,您必须从父视图中删除视频视图mFullScreenViewContainer,然后您可以setContentView()mWebPageParentView同一父视图中使用。

做这样的事情:

ViewGroup parentView = (ViewGroup) mFullScreenViewContainer.getParent();
int index = parentView .indexOfChild(mFullScreenViewContainer);
parentView .removeView(mFullScreenViewContainer);

parentView .addView(mWebPageParentView);

index如果想使用索引删除。和

于 2013-09-11T07:18:35.283 回答