Webview shouldInterceptRequest方法始终返回null

时间:2018-10-19 17:51:41

标签: webview kotlin

Webview shouldInterceptRequest方法始终返回null。我不再使用adblocking方法。我尝试了很多adblock方法,但始终需要此shouldInterceptRequest方法但其返回null。

我的网络视图代码

webView.webViewClient = object : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {




            view!!.loadUrl(request!!.url.toString())
            return true


        }

        override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse {
            return if (

                    AdBlocker.isAd(request!!.url.toString()))
                AdBlocker.createEmptyResource()
            else
                super.shouldInterceptRequest(view, request)
        }



        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            eTadres.setText(url)


            suankiurl= url!!
            super.onPageStarted(view, url, favicon)
        }



        override fun onLoadResource(view: WebView?, url: String?) {
            if (webView.visibility==View.GONE){webView.visibility=View.VISIBLE}
            super.onLoadResource(view, url)
        }


    }

错误日志

W/System.err: java.lang.IllegalStateException: super.shouldInterceptRequest(view, request) must not be null
10-19 17:46:53.185 306-349/com.bulamac.tarayicibulamac W/System.err:     at com.bulamac.tarayicibulamac.WebviewFragment$onCreateView$5.shouldInterceptRequest(WebviewFragment.kt:182)
10-19 17:46:53.185 306-349/com.bulamac.tarayicibulamac W/System.err:     at com.android.webview.chromium.WebViewContentsClientAdapter.shouldInterceptRequest(WebViewContentsClientAdapter.java:52)
10-19 17:46:53.185 306-349/com.bulamac.tarayicibulamac W/System.err:     at org.chromium.android_webview.AwContents$BackgroundThreadClientImpl.shouldInterceptRequest(AwContents.java:9)
10-19 17:46:53.185 306-349/com.bulamac.tarayicibulamac W/System.err:     at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(AwContentsBackgroundThreadClient.java:11)
10-19 17:46:53.186 306-349/com.bulamac.tarayicibulamac A/chromium: [FATAL:jni_android.cc(243)] Please include Java exception stack in crash report

2 个答案:

答案 0 :(得分:1)

super.shouldInterceptRequest(view, request)将始终返回null(请参见此代码的实现)。如果您想防止应用程序崩溃,则应使用以下示例

更改为此:WebResourceResponse? (添加了“?”)

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
            return super.shouldInterceptRequest(view, request)
        }

答案 1 :(得分:0)

super.shouldInterceptRequest(view, request) 预计返回 null,检查您的 override fun shouldInterceptRequest 的返回类型,根据文档,它应该可以为空:

<块引用>

包含响应信息的 WebResourceResponse 或者如果 WebView 应该加载资源本身,则为 null。

相关问题