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

Android WebView Hedgehog版本进度条隐藏后页面黑屏无法加载网站

问题分析与修复方案

1. 布局文件核心错误:SwipeRefreshLayout子View数量违规

SwipeRefreshLayout要求直接子View只能有一个,原布局中直接放置ProgressBar和WebView两个控件,会导致布局渲染异常,WebView无法正常显示。

修复后的activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- 用FrameLayout包裹两个控件,作为SwipeRefreshLayout的唯一子View -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <ProgressBar
                android:id="@+id/progressBar1"
                android:max="100"
                android:progress="0"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />
        </FrameLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>

说明:

  • 用FrameLayout包裹控件,满足SwipeRefreshLayout的单直接子View要求
  • 修正ProgressBar的max/progress值为合理逻辑
  • 用layout_gravity替代原RelativeLayout属性,适配FrameLayout布局规则

2. 网络检测逻辑错误

CheckNetwork类中,网络未连接时错误返回true,导致无网络场景下仍尝试加载WebView,引发加载失败黑屏。

修复后的CheckNetwork类:

class CheckNetwork {
    private static final String TAG = CheckNetwork.class.getSimpleName();
    public static boolean isInternetAvailable(Context context) {
        NetworkInfo info = ((ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if(info == null) {
            Log.d(TAG,"no internet connection");
            return false;
        } else {
            boolean isConnected = info.isConnected();
            Log.d(TAG, isConnected ? "internet connection available…" : "internet not connected");
            return isConnected; // 仅当网络真正连接时返回true
        }
    }
}

3. WebView兼容性补充设置

针对WebView Hedgehog版本,补充必要配置确保页面正常加载,在webview.loadUrl(websiteURL)前添加:

webview.getSettings().setDomStorageEnabled(true); // 开启DOM存储,适配现代网页
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null); // 启用硬件加速提升渲染性能
webview.setVisibility(View.VISIBLE); // 确保WebView默认可见

4. 错误反馈与刷新逻辑修复

  • 增强错误处理,避免黑屏无反馈:
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Log.e("error",description);
    // 加载错误提示页面
    view.loadData("<html><body><h1>加载失败:" + description + "</h1></body></html>", "text/html", "UTF-8");
}
  • 修复刷新状态不停止问题:
@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    mySwipeRefreshLayout.setRefreshing(false); // 停止下拉刷新
    progressBar.setVisibility(View.GONE);
}

内容的提问来源于stack exchange,提问作者s.k.Soni

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 16:05:56