我有一个只有一个WebView的Activity,它包含
HTML,CSS和
Javascript代码.
看来,Javascript访问视图的屏幕尺寸有问题.
LogCat说:
- (Tag: Web Console): Uncaught TypeError: Cannot call method 'getWidth' of undefined
- at file:///android_asset/Prospekte/grund/html5-player/js/epaper-mobile.js:20
当我查看js文件,有:var f = this.body.getWidth();
有好奇的是,有时代码可以工作.电子纸显示得很好.但大多数时候有这个错误.
- setContentView(R.layout.prospekt_layout);
- final Activity activity = this;
- mWebView = (WebView) findViewById(R.id.prospekt_webview);
- mWebView.getSettings().setJavaScriptEnabled(true);
- mWebView.getSettings().setDomStorageEnabled(true);
- mWebView.setWebChromeClient(new WebChromeClient() {
- public void onProgressChanged(WebView view,int progress) {
- activity.setProgress(progress * 1000);
- }
- });
- mWebView.setWebViewClient(new WebViewClient() {
- @Override
- public void onReceivedError(WebView view,int errorCode,String description,String failingUrl)
- {
- // Handle the error
- }
- @Override
- public boolean shouldOverrideUrlLoading(WebView view,String url)
- {
- view.loadUrl(url);
- return true;
- }
- });
- mWebView.loadUrl("file:///android_asset/Prospekte/modKachel/mobile.html");
布局是:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- >
- <WebView
- android:id="@+id/prospekt_webview"
- android:layout_width="900px"
- android:layout_height="900px"
- />
- </LinearLayout>
我改变了webView的大小,因为我认为这可能是解决方案,但它也不适用于dp.
有人有想法?
解决方法
为您的webView设置此设置:
- WebSettings settings = webView.getSettings();
- settings.setDomStorageEnabled(true);
详细信息请参考以下链接中的答案:
ERROR/Web Console: Uncaught TypeError: Cannot call method ‘getItem’ of null at http://m.youtube.com/:844
更新:
或添加此可能有帮助:
- webView.loadDataWithBaseURL("fake://fake.com",myString,"text/html","UTF-8",null);