【发布时间】:2013-05-25 00:51:20
【问题描述】:
我无法让 WebView 显示静态 HTML。它适用于 Android 2.3.3,但不适用于 4.2.2。没有出现类似的问题,因为我没有使用 Javascript、HTTPS,并且我尝试单击视图。我试过文档http://developer.android.com/reference/android/webkit/WebView.html
TmpActivity.java
public class TmpActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tmp);
WebView webView = (WebView) findViewById(R.id.webView1);
webView.loadData("<html><body>You scored <b>192</b> points.</body></html>","texl/html","UTF-8");
}
activity_tmp.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TmpActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp" />
</RelativeLayout>
模拟器运行 2.3.3
模拟器运行 4.2.2
日志中没有错误。唯一的错误是
05-25 00:56:20.563: E/Trace(4140): 打开跟踪文件时出错:没有这样的文件或目录 (2)
【问题讨论】:
-
不是这个问题,但不应该是 text/html(不是 texl/html?)
-
实际上,这很可能是问题所在。当然,
WebView可以通过loadData()显示内容,就像多年来一样,例如在这个示例应用程序中:github.com/commonsguy/cw-omnibus/tree/master/WebKit/Browser3 -
@powerj1984 哦,快! 4小时打错字!诅咒你的安卓!
-
doh,我想我会添加这个作为答案:)
-
@powerj1984 我找到了从哪里复制的:stackoverflow.com/questions/3150400/…
标签: android android-webview android-4.2-jelly-bean