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

Android Studio中WebView加载网页显示空白问题求助

Fixing Blank WebView Issue in Android Studio

Hey there! Let's work through why your WebView is showing a blank screen. I’ve gone through your code and spotted a couple of key issues that are almost certainly causing this problem, plus some extra tips to help you debug further.

Key Issues Identified

1. Typo in Network Permission

In your AndroidManifest.xml, there’s a silly typo in the ACCESS_NETWORK_STATE permission:

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

The correct package name is android (with an extra "n"), not andriod. This typo stops your app from getting the network state access it needs, which can interfere with WebView’s ability to load content properly.

2. Wrong Order of WebView Configuration

You’re calling web.loadUrl(webUrl) before setting up your WebSettings. When you load the URL first, the WebView hasn’t enabled JavaScript or applied other critical settings yet—this leads to incomplete or failed page loading.

3. Redundant Code

You’ve enabled JavaScript twice (mywebsettings.setJavaScriptEnabled(true) appears twice in onCreate). While this doesn’t break functionality, it’s unnecessary and can be cleaned up.

Fixed Code Examples

Updated AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andriodscch.scchemisapp">
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- Fixed the typo in permission name -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/Theme.SCCHEMISAPP">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Updated MainActivity.java

public class MainActivity extends AppCompatActivity {
    private WebView web;
    String webUrl = "https://scchemp.rf.gd/";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web = (WebView) findViewById(R.id.myweb);
        
        // Configure ALL WebSettings first, before loading the URL
        WebSettings mywebsettings = web.getSettings();
        mywebsettings.setJavaScriptEnabled(true);
        // Performance optimizations
        mywebsettings.setAllowFileAccess(true);
        mywebsettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        mywebsettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mywebsettings.setAppCacheEnabled(true);
        mywebsettings.setDomStorageEnabled(true);
        mywebsettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mywebsettings.setUseWideViewPort(true);
        mywebsettings.setSavePassword(true);
        mywebsettings.setSaveFormData(true);
        
        web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        web.setWebViewClient(new WebViewClient());
        
        // Now load the URL after all settings are applied
        web.loadUrl(webUrl);
    }
    
    @Override
    public void onBackPressed() {
        if(web.canGoBack()){
            web.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

Additional Troubleshooting Steps

  • Verify the URL: First, make sure https://scchemp.rf.gd/ loads correctly in a regular browser (like Chrome) on your device. If it doesn’t, the issue is with the website itself, not your code.
  • Check for HTTPS Certificate Issues: If the site uses a self-signed or invalid SSL certificate, WebView might block it. For testing purposes only, you can add a temporary override (never use this in production):
    web.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            // Only use this for testing!
            handler.proceed();
        }
    });
    
  • Enable WebView Debugging: Use Chrome DevTools to inspect what’s happening with the page load. Add this line in onCreate:
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
    
    Then open chrome://inspect in your desktop Chrome browser to debug the WebView directly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 09:12:42