您好我正在制作一个webview应用
我制作了一个webview,我想支持全屏视频
所以我制作自定义视图,但它有错误
错误来自onHideCustomView
如何解决此错误?
logcat的
11-12 21:25:23.340 5340-5340/com.bayabro.bayabro E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
at android.view.ViewGroup.addView(ViewGroup.java:3491)
at android.view.ViewGroup.addView(ViewGroup.java:3467)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:332)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:322)
at android.app.Activity.setContentView(Activity.java:1944)
at com.bayabro.bayabro.MyActivity$MyWebChromeClient.onHideCustomView(MyActivity.java:151)
at com.bayabro.bayabro.MyActivity.onKeyDown(MyActivity.java:174)
at android.view.KeyEvent.dispatch(KeyEvent.java:2705)
at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2028)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3852)
at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3800)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
我的应用完整代码
public class MyActivity extends Activity {
WebView myWebView;
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private RelativeLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setAppCacheEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setDefaultTextEncodingName("EUR-KR");
myWebView.loadDataWithBaseURL(null,"","text/html","utf-8",null);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
myWebView.getSettings().setDomStorageEnabled(true);
mWebChromeClient = new MyWebChromeClient();
//mWebChromeClient = new WMWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
String databasePath = myWebView.getContext().getDir("databases",
Context.MODE_PRIVATE).getPath();
myWebView.getSettings().setDatabasePath(databasePath);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//mwebview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setSupportMultipleWindows(true);
myWebView.loadUrl("http://www.naver.com");
myWebView.setWebChromeClient(new MyWebChromeClient());
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoadding(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
private class MyWebChromeClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
mContentView = (RelativeLayout) findViewById(R.id.www);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MyActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
public void onBackPressed(){
if(mCustomViewContainer!=null)
mCustomViewCallback.onCustomViewHidden();
}
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
public void onPause(){
super.onPause();
myWebView.onPause();
}
public boolean onKeyDown(int key, KeyEvent event) {
if (myWebView != null && mCustomViewContainer != null&&key == KeyEvent.KEYCODE_BACK) {
if(mWebChromeClient == null){
Toast.makeText(MyActivity.this,"hell",Toast.LENGTH_LONG).show();
};
mWebChromeClient.onHideCustomView();
return true;
}
else if(myWebView!=null&&(key == KeyEvent.KEYCODE_BACK)&&myWebView.canGoBack()){
myWebView.goBack();
return true;
}
return super.onKeyDown(key, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}