Webview shouldInterceptRequest方法始终返回null

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Webview shouldInterceptRequest方法始终返回null相关的知识,希望对你有一定的参考价值。

Webview shouldInterceptRequest方法总是返回null。我使用了adblocking的方法。我尝试了很多adblock方法,但总是需要这个shouldInterceptRequest方法,但它返回null。

我的webview代码

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
答案

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

更改为':WebResourceResponse?' ('?' 加入)

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

以上是关于Webview shouldInterceptRequest方法始终返回null的主要内容,如果未能解决你的问题,请参考以下文章

想问开了webview有啥用

electron怎么获取到webview里的元素,webview为加载的网站

webview获取网页点击事件

安卓webview是啥东西

如何监听webview加载完成这个事件

我想问一下开了webview有啥用