You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android WebView加载google.com显示白屏无报错问题求助

WebView加载google.com白屏无报错问题排查

问题概述

开发基于WebView的Android应用,加载https://google.com时始终显示白屏,无任何报错提示。已配置INTERNET权限、网络安全配置并启用cleartextTraffic,Retrofit2网络请求可正常执行,但仅onPageStarted和onPageFinished回调触发,所有错误类回调(如onReceivedError、onReceivedHttpError)均未捕获。

相关代码

MainActivity

private fun initWebView() {
    binding.mainWebView.webViewClient = MyWebClient()
    binding.mainWebView.settings.javaScriptEnabled = true
    binding.mainWebView.settings.cacheMode = WebSettings.LOAD_DEFAULT
    binding.mainWebView.settings.domStorageEnabled = true
    binding.mainWebView.settings.blockNetworkImage = false
    binding.mainWebView.settings.blockNetworkLoads = false
    binding.mainWebView.settings.loadsImagesAutomatically = true
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        binding.mainWebView.settings.safeBrowsingEnabled = false
    }
    binding.mainWebView.settings.javaScriptCanOpenWindowsAutomatically = true
    binding.mainWebView.settings.allowFileAccess = true
    binding.mainWebView.settings.useWideViewPort = true
    binding.mainWebView.fitsSystemWindows = true
    binding.mainWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null)

    binding.mainWebView.loadUrl("https://google.com")
}

MyWebClient

class MyWebClient : WebViewClient() {
    val TAG = "===MyWebClient"
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        view!!.loadUrl(request!!.url.toString())
        return true
    }

    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        view!!.loadUrl(url!!)
        return true
    }

    override fun onReceivedHttpError(
        view: WebView?,
        request: WebResourceRequest?,
        errorResponse: WebResourceResponse?
    ) {
        Log.i(TAG, "onReceivedHttpError: http error ")
        super.onReceivedHttpError(view, request, errorResponse)
    }

    override fun onReceivedError(
        view: WebView?,
        request: WebResourceRequest?,
        error: WebResourceError?
    ) {
        Log.i(TAG, "onReceivedError: ${error!!.errorCode}")
        Log.i(TAG, "onReceivedError: ${error.description}")
    }

    override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
        handler!!.proceed()
        Log.i(TAG, "onReceivedSslError: ${error}")
    }

    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        Log.i(TAG, "onPageStarted: started ${url}")
        super.onPageStarted(view, url, favicon)
    }

    override fun onPageFinished(view: WebView?, url: String?) {
        Log.i(TAG, "onPageFinished: finished ${url}")
        super.onPageFinished(view, url)
    }
}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    ...
    android:networkSecurityConfig="@xml/network_security_config"
    android:usesCleartextTraffic="true"
    ...
/>

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">google.com</domain>
        <domain includeSubdomains="true">www.google.com</domain>
    </domain-config>
</network-security-config>

排查建议

  • 修正URL拦截逻辑:当前shouldOverrideUrlLoading强制拦截所有URL并重新加载,会导致页面跳转循环或加载异常。建议改为仅拦截非目标域名的请求,或直接返回false让WebView自行处理:
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        // 仅拦截非google域名的外部链接,其余交给WebView处理
        return request?.url?.host?.let { !it.contains("google.com") } ?: false
    }
    
  • 禁用硬件加速测试:部分设备上LAYER_TYPE_HARDWARE会导致WebView渲染异常,尝试切换为软件加速:
    binding.mainWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
    
  • 添加WebChromeClient捕获JS日志:WebView的JavaScript错误不会触发WebViewClient的错误回调,添加WebChromeClient可以查看页面内部的JS报错:
    binding.mainWebView.webChromeClient = object : WebChromeClient() {
        override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
            Log.i("WebChromeClient", "JS Log: ${consoleMessage?.message()} (Line: ${consoleMessage?.lineNumber()})")
            return super.onConsoleMessage(consoleMessage)
        }
    }
    
  • 抓包验证请求状态:使用代理工具抓包,确认WebView是否实际发送了请求,以及响应内容是否为空白HTML或正常页面。
  • 测试其他HTTPS站点:如果所在地区无法访问google.com,可尝试加载https://www.baidu.com等可正常访问的站点,验证WebView基础功能是否正常。
  • 补充旧版本错误回调:Android 7.0及以下版本的错误回调为onReceivedError(WebView view, int errorCode, String description, String failingUrl),当前代码仅实现了新版本回调,补充后可捕获低版本的错误信息:
    override fun onReceivedError(view: WebView?, errorCode: Int, description: String?, failingUrl: String?) {
        Log.i(TAG, "Legacy Error: Code=$errorCode, Msg=$description, URL=$failingUrl")
        super.onReceivedError(view, errorCode, description, failingUrl)
    }
    

内容的提问来源于stack exchange,提问作者jose4ka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:55:58