WebView显示白色或空白页面

问题描述 投票:14回答:4

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

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

这个函数也被称为:

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

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

谢谢

android android-webview
4个回答
5
投票

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

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


2
投票

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

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

1
投票

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

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>

Web view client demo activity.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);
       }
    }

在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" />

0
投票

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

webView.setBackgroundColor(Color.TRANSPARENT);
© www.soinside.com 2019 - 2024. All rights reserved.