【问题标题】:Overriding Android WebChromeClient's onCreateWindow method results in SIGSEGV覆盖 Android WebChromeClient 的 onCreateWindow 方法会导致 SIGSEGV
【发布时间】:2011-02-21 18:43:45
【问题描述】:

我试图覆盖默认的 WebChromeClient 以便让我的应用程序的 WebView 能够打开新窗口。为此,按照手册中的说明,我将覆盖 WebChromeClient 的“onCreateWindow”方法,其中我执行以下简单逻辑。

    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;
    }

但这会导致上述分段错误。我做了一些搜索,发现它已经在http://code.google.com/p/android/issues/detail?id=11655 报告了。之后我看不到该问题的任何更新。有人知道相同的状态吗?

谢谢, 阿肖克。

【问题讨论】:

标签: android webchromeclient


【解决方案1】:

如果您在 onCreateWindow 中重复使用 webview,应用会崩溃。

在屏幕布局中使用 ViewGroup 代替 webview,为其提供与 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;
    }

在上面的代码中

1) 我已经设置了布局参数,以便我的 web 视图填充父视图,您应该根据您的要求使用布局参数。 2) mContext => 上下文对象 3) contentContainer => 在 XML 中声明的视图组,旨在包含 Web 视图

这不干净,但解决了问题。

【讨论】:

  • 这对我没有解决,我不确定它是否有意义,如果你删除任何现有的 webviews 那么你不是真的支持多个窗口,是吗?对我来说,我有选择地只在我真正需要的时候才使用settings.setSupportMultipleWindows(true),所以在某些情况下禁用它可以解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
相关资源
最近更新 更多