Javascript无法在WebView Activity中运行

10f*_*0ff 9 javascript android webview

我有一个只有一个WebView的Activity ,它包含HTML,CSS和Javascript代码.

似乎有与问题访问Java脚本屏幕尺寸的看法.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
Run Code Online (Sandbox Code Playgroud)

当我查看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");    
Run Code Online (Sandbox Code Playgroud)

布局是:

 <?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>
Run Code Online (Sandbox Code Playgroud)

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

有人有想法吗?

Usa*_*war 16

为webView设置此设置:

WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅以下链接中的答案: 错误/ Web控制台:未捕获TypeError:无法在http://m.youtube.com/:844调用null的方法'getItem'

更新: 或添加此功能可能有所帮助:

webView.loadDataWithBaseURL("fake://fake.com", myString, "text/html", "UTF-8", null);
Run Code Online (Sandbox Code Playgroud)