【问题标题】:Having blank/white screen on android WebView在 android WebView 上有黑屏/白屏
【发布时间】:2020-12-01 12:04:00
【问题描述】:

首先我想说的是,我可以在 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>

【问题讨论】:

    标签: android android-studio webview


    【解决方案1】:

    试试

    webView.getSettings().setDomStorageEnabled(true)

    【讨论】:

    • 感谢您的回复。但它没有用。
    【解决方案2】:

    对于遇到这种情况的其他人 - 我在重定向到 SSO 登录页面时遇到问题,该页面只是一个空白页面。事实证明,WebViews 默认禁用了 javascript,因此如果您的登录页面使用 javascript,您可能会出现这种症状 - 或其他症状。

    解决我的问题的方法是使用 WebSettings 启用 javascript:

    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    

    【讨论】:

      【解决方案3】:

      在你的清单中试试这个可能是你的问题应该得到解决

      android:usesCleartextTraffic="true"
      

      【讨论】:

      • 此属性仅在 android API 级别 23 之后才允许。但是我的应用程序从 API 级别 21 开始支持。
      • 将您的 api 级别更改为 23,然后检查其是否正常工作
      【解决方案4】:

      我只需更新 Android Studio 和 AVD 即可解决此问题。不知道真正的原因是什么。但是,如果其他人遇到这个奇怪的问题,我认为最好也尝试为其他人更新 Android Studio 和 AVD。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-25
        • 2011-01-11
        • 2018-02-13
        • 2023-03-26
        • 2010-12-31
        • 2019-11-17
        相关资源
        最近更新 更多