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

Android Studio中WebView加载圈不消失问题求助

Fix for Progress Bar Not Disappearing in WebView Splash Screen

Hey there! I spot the exact issue right away—you’ve accidentally overwritten your WebViewClient, which is why the loading spinner never disappears after your webpage finishes loading. Let’s break this down and fix it step by step.

The Root Cause

In your onCreate() method, you first set up your custom CustomWebViewClient (which handles showing/hiding the progress bar and splash screen logic). But then you call setWebViewClient() a second time to handle SSL errors. This second call completely replaces the first WebViewClient instance, meaning your onPageStarted() and onPageFinished() methods in CustomWebViewClient never get triggered. That’s why the spinner stays visible forever!

The Fix: Combine All WebViewClient Logic

Instead of creating two separate WebViewClient instances, merge the SSL error handling into your existing CustomWebViewClient. Here’s the corrected full code:

import android.net.http.SslError;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    String ShowOrHideWebViewInitialUse = "show";
    private WebView myWebView;
    private ProgressBar spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.webView);
        spinner = (ProgressBar) findViewById(R.id.progressBar1);

        // Set ONLY your custom WebViewClient here (no duplicate calls!)
        myWebView.setWebViewClient(new CustomWebViewClient());

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setDatabaseEnabled(true);
        myWebView.getSettings().setMinimumFontSize(1);
        myWebView.getSettings().setMinimumLogicalFontSize(1);

        myWebView.setWebChromeClient(new WebChromeClient());
        myWebView.loadUrl("https://node-red-test.ftp21.net:1024/ui");
    }

    // This handles splash screen logic AND SSL errors now
    private class CustomWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView myWebView, String url, Bitmap favicon) {
            // Only hide the WebView the first time the app launches
            if (ShowOrHideWebViewInitialUse.equals("show")) {
                myWebView.setVisibility(myWebView.INVISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            ShowOrHideWebViewInitialUse = "hide";
            spinner.setVisibility(View.GONE);
            view.setVisibility(myWebView.VISIBLE);
            super.onPageFinished(view, url);
        }

        // Add SSL error handling directly to this custom client
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
            handler.proceed();
        }
    }

    @Override
    public void onBackPressed() {
        if (myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

Key Changes Made:

  • Removed the duplicate myWebView.setWebViewClient() call in onCreate()
  • Moved the onReceivedSslError() method into your CustomWebViewClient class, so all WebView client logic lives in one place
  • Ensured your progress bar visibility logic in onPageFinished() will now execute properly when the webpage finishes loading

Quick Note on Android Studio 2.1.2

While this version is quite outdated, the core issue here is a logical mistake with WebViewClient assignment—not a version-specific bug. That said, if you run into other issues later, keep in mind newer Android Studio versions offer better debugging tools and support for modern Android APIs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:21:31