Javascript不能在WebView活动中工作

前端之家收集整理的这篇文章主要介绍了Javascript不能在WebView活动中工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个只有一个WebView的Activity,它包含 HTML,CSS和 Javascript代码.

看来,Javascript访问视图的屏幕尺寸有问题.
LogCat说:

  1. (Tag: Web Console): Uncaught TypeError: Cannot call method 'getWidth' of undefined
  2. at file:///android_asset/Prospekte/grund/html5-player/js/epaper-mobile.js:20

当我查看js文件,有:var f = this.body.getWidth();

有好奇的是,有时代码可以工作.电子纸显示得很好.但大多数时候有这个错误.

  1. setContentView(R.layout.prospekt_layout);
  2. final Activity activity = this;
  3.  
  4. mWebView = (WebView) findViewById(R.id.prospekt_webview);
  5. mWebView.getSettings().setJavaScriptEnabled(true);
  6. mWebView.getSettings().setDomStorageEnabled(true);
  7. mWebView.setWebChromeClient(new WebChromeClient() {
  8. public void onProgressChanged(WebView view,int progress) {
  9. activity.setProgress(progress * 1000);
  10. }
  11. });
  12.  
  13. mWebView.setWebViewClient(new WebViewClient() {
  14. @Override
  15. public void onReceivedError(WebView view,int errorCode,String description,String failingUrl)
  16. {
  17. // Handle the error
  18. }
  19.  
  20. @Override
  21. public boolean shouldOverrideUrlLoading(WebView view,String url)
  22. {
  23. view.loadUrl(url);
  24. return true;
  25. }
  26. });
  27.  
  28. mWebView.loadUrl("file:///android_asset/Prospekte/modKachel/mobile.html");

布局是:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_height="match_parent"
  5. android:layout_width="match_parent"
  6. >
  7. <WebView
  8. android:id="@+id/prospekt_webview"
  9. android:layout_width="900px"
  10. android:layout_height="900px"
  11. />
  12. </LinearLayout>

我改变了webView的大小,因为我认为这可能是解决方案,但它也不适用于dp.

有人有想法?

解决方法

为您的webView设置此设置:
  1. WebSettings settings = webView.getSettings();
  2. settings.setDomStorageEnabled(true);

详细信息请参考以下链接中的答案:
ERROR/Web Console: Uncaught TypeError: Cannot call method ‘getItem’ of null at http://m.youtube.com/:844

更新:
添加此可能有帮助:

  1. webView.loadDataWithBaseURL("fake://fake.com",myString,"text/html","UTF-8",null);

猜你在找的JavaScript相关文章