![](/img/trans.png)
[英]Displaying a vimeo video in Android's WebView shows a white blank screen
[英]Opening new android activity with a webview shows a blank (white) screen instead of the html content
我已经研究了2天,但在互联网上的任何地方都没有发现类似的问题!
在主要活动中,我试图打开一个包含 webview 的新活动,它将在 android_asset 文件夹中显示一个 html 页面 (about.html)。
我能够在正确显示 webview 的情况下启动新活动 (test.java),但网页内容没有显示。 甚至显示了 webview 的垂直滚动条(因为页面内容相对较长),我可以上下滚动页面但没有内容!
package com.example.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class test extends Activity {
private TextView apppagetitle;
private WebView browser_rn;
@Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.test);
browser_rn=(WebView)findViewById(R.id.webkit);
browser_rn.getSettings().setJavaScriptEnabled(true);
browser_rn.getSettings().setPluginsEnabled(true);
browser_rn.setScrollBarStyle(0);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
apppagetitle=(TextView)findViewById(R.id.apppagetitle);
browser_rn.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
public void onPageFinished(WebView view, String url) {
apppagetitle.setText(browser_rn.getTitle());
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (!isInternetOn()) {
alertDialog.setTitle("Connection Error");
alertDialog.setMessage("You need to be connected to the internet.");
} else {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
}
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
browser_rn.loadUrl("file:///android_asset/about.html");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
browser_rn.clearCache(true);
browser_rn.clearHistory();
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
奇怪的是,当我调用 webview.gettitle() 时,新活动 (test.java) 可以成功获取并在 textview 中显示网页标题,但 html 内容只是没有在 webview 中呈现。 我所看到的只是一个带有滚动条的空白屏幕。 我怀疑可能是布尔值“shouldOverrideUrlLoading”造成的。
感谢您的回复。
测试文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/green"
>
<TextView
android:id="@+id/apppagetitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Odd News"
android:textSize="25px"
android:padding="10px"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
about.html和下面的一样简单。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>About</title>
</head>
<body>
<h3 style="color:#0066dd">About</h3>
<p>Some text here...</p>
</body>
</html>
哦,是的,我之前忘了提到,对于同一段代码,如果我强制停止并重新启动手机上的应用程序,有时会显示 about.html。 但是,它不会出现在随后的发布中。 有时当它成功显示 about.html 并且我按下后退按钮返回主活动时,当我从主活动重新启动活动时它会再次消失。 我迷路了,但我有一种强烈的感觉,我没有正确执行“shouldOverrideUrlLoading”。
不要尝试在shouldOverrideUrlLoading
加载给定的 url,也不要在你的情况下从它返回 true。 真的,从看起来你想要做的事情来看,你根本不需要覆盖它。
从返回true shouldOverrideUrlLoading
指示web视图,它不应该加载指定的URL。 你在做什么 - 告诉 WebView 再次开始加载它 - 可能会导致 WebView 内部出现某种内部循环,我猜这就是你看到奇怪行为的原因。
使用这个
browser_rn.loadDataWithBaseURL(url);
而不是
browser_rn.loadUrl("file:///android_asset/about.html");
我上周在我的 HTC Sensation 上从 2.3.4 更新到 4.0。 我写的两个应用程序都有一个本地的 HTML-Contact 站点。 在 2.3 上它运行良好,现在我得到一个“网页不可用”站点。
我在这个网站上找到了另一篇评论:“通过与 Google 工程师的一些讨论,他们似乎已经决定 file:// 方案不安全。”
解决方法请参见此处: Android 4.0.1 中断 WebView HTML 5 本地存储?
我可以给出另一种方法来解决这个问题。 只需将资产文件复制到数据路径“/data/data/org.test.test/”中,并通过以下方式将其加载到WebView
。
String strHTMLFilePath = "file:///data/data/org.test.test/Files/help_android.html";
webView.loadUrl(strHTMLFilePath);
使用以下代码将 HTML 文件从资产复制到数据路径
void getAssetFiles() {
String WriteFileName = "/data/data/org.test.test/";
try {
String list[] = thisActivity.getAssets().list("Files");
String outFileName = WriteFileName+"Files/";
if (list != null)
for (int i=0; i<list.length; ++i){
if(!new File(outFileName).exists()){
new File(outFileName).mkdirs();
}
OutputStream myOutput = new FileOutputStream(outFileName+list[i]);
byte[] buffer = new byte[1024];
int length;
String strWritePath = "Files" + "/" + list[i];
InputStream myInput = thisActivity.getAssets().open(strWritePath);
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
} catch (IOException e) {
}
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
在您的活动中添加以下行,其中包含 WebView 活动android:hardwareAccelerated="false"
希望它会有所帮助
在text.xml
尝试此更改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".text" > <!-- Here is the change -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/green"
>
<TextView
android:id="@+id/apppagetitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Odd News"
android:textSize="25px"
android:padding="10px"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
可能是由于硬件加速
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
为我工作。
https://developer.android.com/guide/topics/graphics/hardware-accel.html
遇到同样的问题。
我尝试了链接中的解决方案。 它一直失败。
我的情况和它一样。 webview中有H5视频,第一次做的很好,然后就失败了。 当它失败时,我从日志中找到了一些消息
当我调用方法时
mWebView.loadUrl(url)//webview's height is 0px now
//some code change webView's height here
我的解决方案是将 webview 高度设置为 dp 或 match_parent 默认值。 它运作良好!
只需删除您的虚拟设备并创建一个新设备或在您自己的手机上测试即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.