android webview中的JS确认框不起作用 [英] JS confirm box in android webview not working

查看:117
本文介绍了android webview中的JS确认框不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码加载url.Url在android&中工作正常桌面浏览器. 我已经编写了Android代码来显示android中的Confirm框.在Nexus和Samsung设备上运行正常,但通过在控制台中显示错误

Using this code to load url.Url is working fine in android & desktop browser. I have write Android code to show Confirm boxes in android. It's working fine in Nexus and Samsung device but no Confirm boxes shown in Huawei device by giving error in console

未捕获的TypeError:无法调用null的方法'querySelector'",来源: http://abc/build/js/frontend-abc.js (16683)

"Uncaught TypeError: Cannot call method 'querySelector' of null", source: http://abc/build/js/frontend-abc.js (16683)

private class WebViewChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
            new AlertDialog.Builder(context)
                    .setTitle(getString(R.string.str_confirmation_title))
                    .setMessage(message)
                    .setPositiveButton(getString(R.string.str_ok),
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    result.confirm();
                                }
                            }).setCancelable(false).create().show();

            return true;
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            new AlertDialog.Builder(context)
                    .setTitle(getString(R.string.str_confirmation_title))
                    .setMessage(message)
                    .setPositiveButton(getString(R.string.str_ok),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    result.confirm();
                                }
                            }).setNegativeButton(getString(R.string.str_cancel),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.cancel();
                        }
                    }).create().show();
            return true;
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
            final LayoutInflater factory = LayoutInflater.from(context);
            final View v = factory.inflate(R.layout.layout_alertdialog, null);
            ((TextView) v.findViewById(R.id.tv_messagealert)).setText(message);
            showJSPromptAlert(v, result);
            return true;
        }
}

private void showJSPromptAlert(View v, final JsPromptResult result) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(getString(R.string.str_confirmation_title))
                .setView(v)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                result.confirm(getString(R.string.str_ok));
                            }
                        })
                .setNegativeButton(android.R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                result.cancel();
                            }
                        })
                .setOnCancelListener(
                        new DialogInterface.OnCancelListener() {
                            public void onCancel(DialogInterface dialog) {
                                result.cancel();
                                dialog.cancel();
                            }
                        });
        alert11 = builder.create();
        alert11.show();
    }
 WebView wvContainer = (WebView) findViewById(R.id.wv_container);
 private void loadUrl(String url) {
        wvContainer.setInitialScale(1);
        wvContainer.setWebViewClient(new MyBrowser());
        adjustWebViewSettings();
        wvContainer.canGoBack();
        adjustWebViewForLatestVersion();
        wvContainer.setWebChromeClient(new WebViewChromeClient());
        wvContainer.loadUrl(url);
    }

    private void adjustWebViewSettings() {
        wvContainer.getSettings().setJavaScriptEnabled(true);
        wvContainer.getSettings().setSupportZoom(true);
        wvContainer.getSettings().setAllowContentAccess(true);
        wvContainer.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wvContainer.getSettings().setSupportMultipleWindows(true);
        wvContainer.getSettings().setDomStorageEnabled(true);
        wvContainer.getSettings().setAppCacheEnabled(true);
        wvContainer.getSettings().setUseWideViewPort(true);
        if (Build.VERSION.SDK_INT >= 21) {
            wvContainer.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    private void adjustWebViewForLatestVersion() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            wvContainer.getSettings().setAllowUniversalAccessFromFileURLs(true);
            wvContainer.getSettings().setAllowFileAccessFromFileURLs(true);
            wvContainer.getSettings().setAllowFileAccess(true);
            wvContainer.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

推荐答案

"Uncaught TypeError: Cannot call method 'querySelector' of null"基本上意味着Web视图中的JS代码尝试访问被解释为null的内容的querySelector对象.

"Uncaught TypeError: Cannot call method 'querySelector' of null" basically means that JS code in the webview tries to access the querySelector object of something that is interpreted as null.

Null是基元,没有属性.因此,您可能会遇到一些编程错误,该错误依赖于A的存在来填充B,但A似乎不存在(querySelector是文档或元素中的方法).

Null is a primitive and doesn't have properties. So you probably have some programming error that relies on the existence of A for B to be populated but A doesn't seem to exist (querySelector is a method in document or elements).

尝试获取设备错误的JS堆栈跟踪,弄清楚为什么应提供querySelector的事物(文档或元素)为空.然后解决这个问题,看看一切是否顺利...

Try to obtain a JS stack trace of the device errors an figure out why the the thing that SHOULD provide querySelector (document or an element) is null. Then solve that issue, and see if all shines well...

  • 当代码在其上调用方法时,文档未完全加载
  • 特定于设备的代码变坏了
  • 墨菲定律...

这篇关于android webview中的JS确认框不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆