【问题标题】:Webview showing nothing white screenWebview 不显示任何白屏
【发布时间】:2018-04-19 15:37:40
【问题描述】:

Android webview显示白屏而不是加载网站。当我从网络浏览器打开网站时,网站完美打开,我使用以下代码这是代码请帮助我该怎么做

AndroidMainifest.xml

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

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

    <application
        android:debuggable="true"
        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>
    </application>

</manifest>

activity_main.xml

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

包装在.aditya.aditya;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView webview;

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

        webview =(WebView)findViewById(R.id.webView);

        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.loadUrl("https://www.pdf2jpg.net");
    }
}

【问题讨论】:

  • 第一次尝试加载简单的网址,例如 www.google.com,它是否有效
  • 检查另一个网站
  • 你的代码在我的设备上工作正常,确保你的设备有互联网连接
  • 是的,我有互联网连接
  • 在 WebViewClient 中实现覆盖方法并检查它们的状态

标签: android webview


【解决方案1】:

您可以使用 webview.setWebViewClient() 方法来检查 url 是否正在加载。它具有覆盖方法,如 onPageFinished()onPageStarted() 等...!!

试试下面的代码:

webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.d("Log-->> ", "onPageStarted: ");
            }

            public void onPageFinished(WebView view, String url) {
                Log.d("Log-->> ", "onPageFinished: ");

            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                Log.d("Log-->> ", "onReceivedError: ");
            }

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                Log.d("Log-->> ", "onReceivedHttpError: ");
            }
        });

您可以检查日志以查看您的 URL 是否正确加载。

【讨论】:

    【解决方案2】:

    确保您已在清单中添加互联网权限

    并检查系统 android webview 是否已更新。早些时候发现了一些问题,例如空白页

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-17
      • 2013-08-25
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2018-02-13
      相关资源
      最近更新 更多