Android WebView 显示白色或空白页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10380358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:28:12  来源:igfitidea点击:

WebView Display White or Blank page

androidandroid-webview

提问by Rendy

does anyone know why webview displaying white page? Here is the code

有谁知道为什么 webview 显示白页?这是代码

webView = (WebView) this.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(THE_URL);
webView.setWebViewClient(new WebViewClient());

and this functions is also called:

这个函数也被称为:

@Override
public void onPageFinished(WebView view, String url) {
}

am I missing something there? Of course, the error is not from the XML, because the webview only does not display some URL.

我在那里错过了什么吗?当然,错误不是来自XML,因为webview只是不显示一些URL。

Thanks

谢谢

回答by Dom

For anyone still having this issue, it seems that the WebViewClient does not warn you if a secure connection via https has failed. This is likely due to an SSL error on the server you are connecting to.

对于仍然存在此问题的任何人,如果通过 https 的安全连接失败,WebViewClient 似乎不会警告您。这可能是由于您正在连接的服务器上的 SSL 错误。

You can try using http instead if the server allows it.

如果服务器允许,您可以尝试改用 http。

回答by Shankar Agarwal

Refer this link. In your code you are creating two webview clients simultaneously.

请参阅此链接。在您的代码中,您同时创建两个 webview 客户端。

mwebview=(WebView)findViewById(R.id.webview);
mwebview.getSettings().setJavaScriptEnabled(true);
mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mwebview.loadUrl(webUrl);
mwebview.setWebChromeClient(new WebChromeClient() );

回答by Aerrow

Instead of your activity try this,

而不是你的活动试试这个,

main.xml

主文件

<!--?xml version="1.0" encoding="utf-8"?-->
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">

   <Textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="This is the demo of WebView Client" android:textsize="20sp" android:gravity="center_horizontal">
   </Textview>

   <Webview android:id="@+id/webview01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1">
   </Webview>

   <Imageview android:src="@drawable/ic_launcher" android:layout_height="wrap_content" android:layout_width="fill_parent">      

</Imageview></Linearlayout>

WebViewClientDemoActivity.java

WebViewClientDemoActivity.java

   import android.app.Activity;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    /*
     * Demo of creating an application to open any URL inside the application and clicking on any link from that URl
    should not open Native browser but  that URL should open in the same screen.
     */
    public class WebViewClientDemoActivity extends Activity {
        /** Called when the activity is first created. */

        WebView web;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            web = (WebView) findViewById(R.id.webview01);
            web.setWebViewClient(new myWebClient());
            web.getSettings().setJavaScriptEnabled(true);
            web.loadUrl("http://www.google.com");
        }

        public class myWebClient extends WebViewClient
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub

                view.loadUrl(url);
                return true;

            }
        }

        // To handle "Back" key press event for WebView to go back to previous screen.
       @Override
       public boolean onKeyDown(int keyCode, KeyEvent event)
      {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
       }
    }

In your manifest.xmluse these permisions

在您的manifest.xml 中使用这些权限

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

回答by Arda

Adjust your container view background and then set

调整您的容器视图背景,然后设置

webView.setBackgroundColor(Color.TRANSPARENT);