Overriding Android WebChromeClient's onCreateWindow method results in SIGSEGV

杀马特。学长 韩版系。学妹 提交于 2019-12-13 02:57:53

问题


I tried to override the default WebChromeClient in order to get give my application's WebView the ability to open new windows. For this, as instructed in the manual, I'm overriding the 'onCreateWindow' method of WebChromeClient wherein I do the following simple logic.

    public boolean onCreateWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {

        ((WebView.WebViewTransport) resultMsg.obj).setWebView(myWebView);
        Log.d("webviewdemo", "from the chrome client");
        resultMsg.sendToTarget(); 
        return true;
    }

But this results in the above mentioned segmentation fault. I did some search & found that it's already reported at http://code.google.com/p/android/issues/detail?id=11655. I don't see any updates to that issue after that. Does somebody know the status of the same?

Thanks, Ashok.


回答1:


The app crashes if you reuse a webview in onCreateWindow.

Instead of webview, use a ViewGroup in the screen layout, give it the same layout parameters (location, size etc) as you gave the webview (mWebViewPopup).

    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
    {
        contentContainer.removeAllViews();

        WebView childView = new WebView(mContext);
        childView.getSettings().setJavaScriptEnabled(true);
        childView.setWebChromeClient(this);
        childView.setWebViewClient(new WebViewClient());
        childView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        contentContainer.addView(childView);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(childView);
        resultMsg.sendToTarget();
        return true;
    }

in the above code

1) I have set layout parameters so that my web views fills the parent, you should use layout parameters as per your requirement. 2) mContext => context object 3) contentContainer => viewgroup which was declared in XML intended to contain the web view

This is not clean but solves the problem.



来源:https://stackoverflow.com/questions/5069901/overriding-android-webchromeclients-oncreatewindow-method-results-in-sigsegv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!