[英]Unable to display a html file a blank screen appears
我在res下的原始文件夾中有一個演示文件。 當我單擊按鈕打開文件時,出現空白屏幕,我無法解決問題。
這是代碼:
對於xml文件:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
對於活動:
package com.ashsoft.basiccprogram;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class First extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
WebView webView = (WebView) this.findViewById(R.id.webView1);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadData(readTextFromResource(R.raw.demo), "text/html", "utf-8");
}
private String readTextFromResource(int resourceID)
{
InputStream raw = getResources().openRawResource(resourceID);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int i;
try
{
i = raw.read();
while (i != -1)
{
i = raw.read();
}
raw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return stream.toString();
}
};
我出現黑屏。 demo.html保存在原始文件夾中。
將demo.html
移到assets
文件夾中並使用:
webView.loadUrl("file:///android_asset/demo.html");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.