Android WebView 显示空白页面 [英] Android WebView shows blank page

查看:55
本文介绍了Android WebView 显示空白页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题仅发生在某些设备上.我的应用程序有大约 190 万用户,作为一些用户的反馈,我收到了这个问题.他们也发送了屏幕截图.

The problem occurs only some devices. My application has about 1.9 million users, I get this problem as feedback from some users. They have sent screen shots too.

问题详细说明:WebView 在某些设备上无法显示我的静态 HTML 内容,只显示空白页面.但是 99% 的设备都可以正常工作.

Detailed Explanation of problem: The WebView cannot show my static HTML content on some devices, shows only blank page. But 99% of devices works fine.

根据用户反馈,这个问题出现在许多不同的品牌上,如三星、索尼、LG、中兴等.而且他们有不同版本的 Android 操作系统,从 4.0.3 到 5.1.1

According to user feedbacks, this problem occurs on many different brands like Samsung, Sony, LG, ZTE etc. Also they have different versions of Android OS from 4.0.3 to 5.1.1

尝试了所有 Genymotion 模拟器和许多真实设备,都非常适合我.从来没有亲自见过这个错误.但有些用户在 1.5 年之内一直报告这个问题.

Tried all Genymotion Emulators and many real devices, all worked perfectly for me. Never seen this bug personally. But some users keep reporting this issue for 1.5 years.

我的 WebView 的 layout_height 参数也是 wrap_content,但它的行为就像有一些不可见的内容(几行).通常,它应该有很多行.

Also my WebView's layout_height parameter is wrap_content, but it behaves like it has some invisible content(a couple of lines). Normally, it should have lots of lines.

我的网络视图设置:

webView = (WebView) v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(wvClient);
webView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);

String start = "<html><head><meta name=\"viewport\" content=\"user-scalable=no\"/><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /><style>a {color:#9b252e;}\nimg.size-full{width:100%; height:auto;} iframe{width:100%; height:auto;} img{display: inline; height: auto; max-width: 100%;}</style></head><body>";
String end = "</body></html>";

webView.loadDataWithBaseURL(null, start + myHTMLContent + end, null, "text/html; charset=UTF-8", null);

我的问题是:

  1. 什么会导致这个问题?
  2. 我该如何解决这个问题?
  3. 是否与设备上当前安装的 WebView 版本有关?

我已经检查了所有相关问题&stackoverflow 上的答案.

I have already checked every related questions & answers on stackoverflow.

推荐答案

我首先要修复 HTML,使其中没有错误.不知道你放的是什么内容,但是header里面已经有几个错误了:

What I would start with is fix the HTML so that there are no errors in it. I don't know which content you put in it, but the header already has several errors in it:

  • 没有文档类型;
  • 您应该编写 content='text/html;charset='UTF-8' 而不是 content='text/html' charset='UTF-8';
  • 没有标题元素.

我倾向于认为问题与您在内部显示的内容有关,因为外部保持不变,因此也请确保没有错误.

I'm inclined to think that the problem is related to the content you are showing inside since the outside stays the same, so make sure there are no errors as well.

虽然这可能与WebView的版本有关,但既然你提到问题发生在4.0,我认为应该不仅仅是WebView<的一个版本/code> 因为在那个版本的 Android 上,它不像最新版本那样从 Google Play 更新.

While this may be related to the version of the WebView, but since you mention that the problem happens on 4.0, I think it should be more than just a version of a WebView because on that version of Android it isn't updated from Google Play like on the newest versions.

这篇关于Android WebView 显示空白页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆