在Android WebView上出现空白/白屏问题

4
首先,我想说在Stack Overflow和许多其他地方都可以看到很多类似于我的问题的问题。然而,我仍然无法从它们中找到解决我的问题的方法,因此我将我的问题放在这里。
我正在编写一个带有WebView的应用程序,在加载URL时出现白屏。它之前工作得很好并且加载了网页。但是现在它只给了一个空白屏幕,并且似乎没有加载页面。甚至在我提供错误的URL时,它也不会给出onReceivedError()方法中定义的消息。目前我正在使用android API 22的虚拟设备进行测试。以下是我的代码。有人能建议我错误在哪里以及如何修复它吗?
主要活动
   package com.test.testApp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.Window;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class MainActivity extends AppCompatActivity {
        private WebView webView;
        private static final String URL = "https://www.google.com/";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getSupportActionBar().hide();
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new MyWebViewClient());
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
    
            webView.loadUrl(URL);
        }
    
        @Override
        public void onBackPressed() {
            if (webView.canGoBack()) {
                webView.goBack();
            } else {
                super.onBackPressed();
            }
        }
    
        public class MyWebViewClient extends WebViewClient {
    
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                String htmlData = "<html><body><div align=\"center\">Something wrong happened. Please check your internet connection and try again...!</div></body>";
                webView.loadUrl("about:blank");
                webView.loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null);
            }
        }
    }

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">
        
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent">
        
          <WebView
              android:id="@+id/webView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layerType="hardware">
          </WebView>
         </LinearLayout>
        
        </androidx.constraintlayout.widget.ConstraintLayout>
    

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.testApp">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SettingsActivity">
            <intent-filter>
                <action android:name="com.test.testApp.SettingsActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>
4个回答

1

try

webView.getSettings().setDomStorageEnabled(true)


谢谢回复。但是它没有起作用。 - Jagath Bandara

0
对于其他遇到此问题的人 - 当我被重定向到SSO登录页面时,我遇到了一个问题,页面只是一个空白页面。原来WebViews默认情况下禁用javascript,因此如果您的登录页面使用javascript,则可能会出现此症状或其他症状。
解决我的问题的方法是使用WebSettings启用javascript:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);

-1

尝试在您的清单中添加这个,也许可以解决您的问题

android:usesCleartextTraffic="true"

此属性仅适用于Android API级别23及以上。然而,我的应用程序支持API级别21及以上。 - Jagath Bandara
将您的API级别更改为23,然后检查它是否正常工作。 - Syed Rafaqat Hussain

-2

我成功解决了这个问题,只需要更新一下Android Studio和AVD就可以了。不知道具体原因是什么,但是我觉得如果其他人也遇到了这个奇怪的问题,最好也尝试一下更新Android Studio和AVD。


这不是正确的答案。 问题在于webview设置。 - Edhar Khimich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接