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

Android Studio 2023开发WebView应用手机端白屏问题求助

Android WebView 白屏问题排查方案

我在Android Studio 2023中开发了一个WebView应用,但在手机上显示白屏。已启用JavaScript,且Manifest文件中配置了网络权限,却仍无法正常加载页面。相关代码如下:

MainActivity.java

package com.cmf.deneme;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

       WebView myWebView = findViewById(R.id.yen);

        // WebViewClient ekleyerek web sayfasını uygulamanızın içinde açın
        myWebView.setWebViewClient(new WebViewClient());

        myWebView.getSettings().setJavaScriptEnabled(true); // İhtiyaca bağlı, JavaScript'i etkinleştirmek istiyorsanız

        myWebView.loadUrl("http://www.example.com");
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

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

</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
        android:allowBackup="true"
                android:dataExtractionRules="@xml/data_extraction_rules"
                android:fullBackupContent="@xml/backup_rules"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/Theme.Deneme"
                tools:targetApi="31">
<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>

排查步骤:

  • 解决HTTP明文限制:Android 9(API 28)及以上默认禁止HTTP明文请求,你加载的http://www.example.com属于HTTP协议。在AndroidManifest.xml的<application>标签中添加:

    android:usesCleartextTraffic="true"
    
  • 添加错误监听:仅设置WebViewClient无法捕获加载错误,添加WebChromeClient监听错误日志,修改MainActivity代码:

    import android.webkit.WebChromeClient;
    import android.util.Log;
    import android.webkit.ConsoleMessage;
    
    // ... 其他代码不变
    
    myWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Log.e("WebViewError", "Code: " + errorCode + ", Desc: " + description);
        }
    
        @Override
        public void onConsoleMessage(ConsoleMessage consoleMessage) {
            super.onConsoleMessage(consoleMessage);
            Log.d("WebViewConsole", consoleMessage.message() + " (Line " + consoleMessage.lineNumber() + ")");
        }
    });
    

    运行后查看Logcat的错误信息,定位具体问题。

  • 验证WebView可见性:给WebView添加背景色(如android:background="#FF0000"),确认WebView是否正常显示在界面上,排除布局遮挡问题。

  • 检查网站可达性:用测试手机的自带浏览器打开http://www.example.com,确认网络和网站本身无问题。

  • 启用WebView调试:在MainActivity的onCreate方法开头添加:

    WebView.setWebContentsDebuggingEnabled(true);
    

    然后在电脑端Chrome浏览器输入chrome://inspect,连接手机后查看WebView的加载状态、控制台错误,这是最直接的排查方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 12:35:23