38

我有一个 android 应用程序,我正在使用在 android 2.3.3 上运行的模拟器开发一个嵌入 WebView 的框架布局,该框架布局嵌套在线性布局(垂直)中。无论我使用什么代码,它实际上都不会做任何事情。我将 android 应用程序的权限设置为 INTERNET。即使尝试仅加载 HTML 代码。我得到一个空白的白页,没有错误,什么都没有。logcat 中也没有任何内容。

WebView wview = (WebView)findViewById(R.id.webView1);
wview.getSettings().setJavaScriptEnabled(true);

我已经尝试了所有这三种方法来尝试将任何内容加载到 webview 中:

wview.loadUrl("http://www.google.com");
wview.loadData("<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8");
wview.loadDataWithBaseURL(null, "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8",null);

这三个都给了我相同的结果。没有什么。

有任何想法吗?

4

15 回答 15

73
WebSettings settings = wbView.getSettings();
settings.setDomStorageEnabled(true);
于 2015-06-25T02:59:18.623 回答
14

可能有点晚了,但我想和你分享我的经验,因为我遇到了同样的问题并且花了很多时间。

把你的WebViewinRelativeLayout而不是 in FrameLayout

我测试过的内容:每次都调用我WebView的,但在屏幕上我得到了空白页。onPageFinished()

chrome://inspect WebView调试器中,我可以WebView通过按下inspect按钮“ping”我的,然后WebView立即刷新。

我的想法 -WebView在 中未正确呈现FrameLayout,并RelativeLayout解决了问题(甚至invalidate()调用requestLayout()也没有帮助)。

于 2015-06-02T15:10:41.453 回答
12

我想如果你编辑你的一些代码就可以了。看看我在下面的例子中是怎么做的。用这种方式为你尝试..
用于显示网页

final WebView wbView = (WebView) findViewById(R.id.WebView);

WebSettings settings = wbView.getSettings();
settings.setJavaScriptEnabled(true); 
wbView.loadUrl("https://play.google.com/store/apps");
wbView.clearView();
wbView.measure(100, 100);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);

要显示 HTML:
请参阅本教程..

于 2013-01-13T07:49:35.050 回答
9

而不是传递null,你应该传递一个空String给它。例如:""
所以你应该这样称呼它:
wview.loadDataWithBaseURL("", "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8","");

于 2013-01-13T07:01:04.097 回答
4

我给 +1 是因为这些都是使 webview 正常工作的有效帮助,但这不是我问题的解决方案。

我没有意识到我的 webview 布局包含一个 wrap_content,这导致它的高度为 0px。通过将其设置为 fill_parent 我解决了这个问题。(带有白色背景的 Webview 和带有白色背景的应用程序,当它在布局设计器中显示为全高时,您无法真正分辨出它不是全高。)

不过感谢您的帮助!

于 2013-03-24T20:45:54.637 回答
4

clearView已弃用。

利用:

webView.loadUrl("about:blank")
于 2014-09-04T17:03:50.970 回答
4

就我而言,这是因为我的 webview 试图使用 HTTPS 加载证书不受信任的页面。

您可以使用以下代码段检查您的代码中是否存在这种情况:

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed();
}

爪哇

override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
    handler?.proceed()
}

科特林

然后,如果是这种情况,请快速删除该片段并参考文档以进一步诊断问题,并可能将证书添加到您的密钥库中。

于 2019-11-18T10:28:07.480 回答
3

如果它仍然不起作用,请尝试在网址前添加“http://”“https://”,它应该可以工作。

于 2018-09-24T23:08:13.507 回答
3

在 webview 布局中将 wrap_content 更改为 fill_parent。它对我有用

于 2016-07-28T23:41:04.433 回答
1

我认为您需要的是 WebViewClient。设置 webviewclient 后调用 webview.loadUrl(_url); 像这样的东西....

private void loadUrlInWebView(String _URL){
    WebView myWebView = (WebView) findViewById(R.id.webviewer);     

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);

    myWebView.setWebViewClient(new MyWebViewClient());
    myWebView.loadUrl(_URL);
}

private class MyWebViewClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {         

            view.loadUrl(url);

        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
    }
}
于 2014-10-31T03:52:29.900 回答
1

使用 ip 地址而不是 web url 然后使用以下 ssl 错误处理方法覆盖 webclient

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // Ignore SSL certificate errors
}
于 2018-12-21T21:21:35.387 回答
0

You can use the following code, this will be helpful:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
于 2014-03-31T06:29:03.060 回答
0

尝试增加模拟设备的RAM。这为我解决了问题。

我通过查看 Android Monitor (Android Studio) 中的 logcat 消息发现了这一点。下面的消息让我知道增加RAM有什么问题:

W/art: Throwing OutOfMemoryError "Failed to allocate a 6635532 byte allocation with 1170528 free bytes and 1143KB until OOM"

W/JavaBrowserViewRendererHelper: Error allocating bitmap

E/chromium: [ERROR:java_browser_view_renderer_helper.cc(132)] Error unlocking java bitmap pixels.
于 2017-07-04T08:55:33.657 回答
0

我的 webview上面显示了一个白色的空白方块,有时完全空白。这仅在加载某些数据时发生。

将其更改为RelativeLayout,并将 layout_height 设置为match_parent对我有用。

<RelativeLayout xmlns:android=...
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/ll_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="3dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        .....

    </RelativeLayout>

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ll_title"
        android:background="@color/colorYellow" />


    <ProgressBar
        android:id="@+id/pb_spinning"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

</RelativeLayout>
于 2018-10-15T20:41:18.463 回答
0

只需使用 loadDataWithBaseURL

        loadDataWithBaseURL(null,"html content" , "text/html" , "utf-8",null)
于 2021-10-26T11:55:25.780 回答