Android WebView没有加载Javascript时出错

时间:2016-12-14 18:37:55

标签: android webview

我有一个Android WebView加载一个带有Javascript(AngularJS)代码的页面。除了循环进度条显示之外,具有Javascript代码的页面根本不显示。在Logcat上我看到了这个错误:

I/chromium: [INFO:CONSOLE(117)] "TypeError: Cannot read property 'length' of undefined

有没有办法处理此错误并仍然可以加载页面?它正在桌面Chrome浏览器上工作,页面正常加载。

目前,我已在我的webView实例上完成以下操作:

   webView = (WebView) findViewById(R.id.web_view);
    webView.setWebViewClient(webViewClient);
    webView.setWebChromeClient(webChromeClient);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Log.i(getClass().toString(), "loading link in self");
        return true;
    }

    public void onPageFinished(WebView view, String url) {
        Log.i(getClass().toString(), " page finished");
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        if(url.startsWith("http://example.org/pdflinks")) {
            Log.i(getClass().toString(), "loading in Google PDF viewer url: " + url);
            loadPdfItem(url);
        }
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        Log.e(getClass().toString(), "error: " + error);
        Toast.makeText(getApplicationContext(), "Error: " + error, Toast.LENGTH_LONG).show();
        //super.onReceivedError(view, request, error);
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        Log.e(getClass().toString(), "received http error: " + errorResponse);
        Toast.makeText(getApplicationContext(), "Error: " + errorResponse, Toast.LENGTH_LONG).show();
        //super.onReceivedHttpError(view, request, errorResponse);
    }
}

class MyWebChromClient extends WebChromeClient {
    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
        Log.i(getClass().toString(), "console.message: " + consoleMessage.message());
        return false;
        //return super.onConsoleMessage(consoleMessage);
    }

}

感谢任何见解。

0 个答案:

没有答案