Android WebView 显示空白页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2568063/
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 06:21:19  来源:igfitidea点击:

Android WebView shows a blank page

androidwebview

提问by Sara

Trying to create a WebView but it only shows a blank/white page. I have followed several examples and they all say that work with this code...

试图创建一个 WebView,但它只显示一个空白/白页。我遵循了几个示例,他们都说可以使用此代码...

Here is my code:

这是我的代码:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class PostenWebView extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.web_view);
        WebView webview = (WebView)findViewById(R.id.webview);
        webview.loadUrl("http://www.google.com");
    }
}

And here is the web_view.xml:

这是 web_view.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">
    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>

回答by CommonsWare

You need to enable Javascript (.getSettings().setJavaScriptEnabled(true)), or choose a Web page that does not rely upon Javascript.

您需要启用 Javascript ( .getSettings().setJavaScriptEnabled(true)),或选择不依赖 Javascript 的网页。

回答by Pentium10

You have to add the permission to your AndroidManifest.xml file.

您必须将权限添加到您的 AndroidManifest.xml 文件中。

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

回答by Jaya

Use webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); method

使用 webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 方法

回答by jsancheh

It's works fine for me

这对我来说很好用

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

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});

webView.loadUrl("http://www.google.com");

Good Luck!

祝你好运!

回答by Sara

I found an example that finally worked! :)

我找到了一个终于奏效的例子!:)

https://apps-for-android.googlecode.com/svn/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

https://apps-for-android.googlecode.com/svn/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

But the problem was also with the URL, when I tried to go to google.com through my WebView in instantly opened my ordinary browser on my phone... But other URL's worked just fine :)

但问题也出在 URL 上,当我尝试通过 WebView 访问 google.com 时,我立即在手机上打开了普通浏览器......但其他 URL 工作得很好:)

回答by Abhinava

You might be getting redirected.. just install a webviewclient allong with you web view :)

您可能会被重定向.. 只需在您的网络视图中安装一个 webviewclient :)

回答by umar

The quick and dirty solution can be when your android app get started set the background colour according to web view eg i am using black colour so myWebView.setBackgroundColor(Color.parseColor("#000000"));

快速而肮脏的解决方案可能是当您的 android 应用程序开始时根据网络视图设置背景颜色,例如我使用黑色所以 myWebView.setBackgroundColor(Color.parseColor("#000000"));

回答by Ayreon

if you try to load a HTTPS URL (e.g. Foursquare authentication URL) do not forget to call

如果您尝试加载 HTTPS URL(例如 Foursquare 身份验证 URL),请不要忘记调用

webview.clearSslPreferences();

webview.clearSslPreferences();

before trying to load that

在尝试加载之前