在我的Android应用程序中,我使用了一个html页面。当我尝试调用html页面时。它给我一个像
这样的错误 W/webcore(23533): Can't get the viewWidth after the first layout
此处带有webview的XML文件,用于Android中的加载html页面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webViewMyBook"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
这里是webview中显示html页面的代码
public class MyBookActivity extends Activity {
Context myContext;
GeneralHelper objHelper;
WebView myBookWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.gc();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.my_book_layout);
myContext = this;
objHelper = new GeneralHelper(myContext);
myBookWebView = (WebView) findViewById(R.id.webViewMyBook);
myBookWebView.setBackgroundColor(Color.parseColor("#FFFCA9"));
myBookWebView.setHorizontalScrollBarEnabled(false);
myBookWebView.setVerticalScrollBarEnabled(false);
myBookWebView.getSettings().setJavaScriptEnabled(true);
final JavaScriptHandler objScriptHandler1 = new JavaScriptHandler(
myContext, MyBookActivity.this);
myBookWebView.addJavascriptInterface(objScriptHandler1,
"AndroidFunction");
myBookWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
final android.webkit.JsResult result) {
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
}).setCancelable(true).show();
return true;
};
});
myBookWebView.loadUrl("file:///android_asset/mybook.html");
}
}
答案 0 :(得分:0)
我取消了addJavascriptInterface的调用。 在这种情况下,一切都正常。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MyBookActivity extends Activity {
Context myContext;
// GeneralHelper objHelper;
WebView myBookWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.gc();
requestWindowFeature( Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.my_book_layout);
myContext = this;
// objHelper = new GeneralHelper(myContext);
myBookWebView = (WebView) findViewById(R.id.webViewMyBook);
myBookWebView.setBackgroundColor( Color.parseColor( "#FFFCA9" ));
myBookWebView.setHorizontalScrollBarEnabled(false);
myBookWebView.setVerticalScrollBarEnabled(false);
myBookWebView.getSettings().setJavaScriptEnabled(true);
// final JavaScriptHandler objScriptHandler1 = new JavaScriptHandler(myContext, MyBookActivity.this);
// myBookWebView.addJavascriptInterface(objScriptHandler1, "AndroidFunction");
myBookWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
final android.webkit.JsResult result) {
new AlertDialog.Builder(view.getContext())
.setMessage( message )
.setPositiveButton( android.R.string.ok, new AlertDialog.OnClickListener() {
public void onClick( DialogInterface dialog, int which ) {
result.confirm();
}
} ).setCancelable(true).show();
return true;
};
});
myBookWebView.loadUrl("file:///android_asset/mybook.html");
}
}
资产文件夹中的mybook.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="test.css" />
<title>Insert title here</title>
</head>
<body>
<H1>TEST.HTML</H1>
</body>
</html>