javascript - 未捕获的类型错误 : Cannot set property 'value' of > null"in Android

标签 javascript android webview android-webview android-4.4-kitkat

我尝试通过Android端的java脚本代码更新信息。

在 Android 版本 4.3 上,它可以工作。

但是Android 4.4版本,出现了这些错误,所以无法更新信息。

[INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source: (1) I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source: (1) I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot call method 'submit' of null", source: (1)

我从 Android 4.4 版本知道,已经使用新的 Web View支持,但我不知道主要问题在哪里。

即使在 Android 4.4 上也知道如何通过 Java 脚本代码成功更新信息的人,

请告诉我,

谢谢你,

p/s : 代码

// Load Web View to update new SSID, Security Key, Security Mode
            WebView mWv = new WebView(this);
            // Simplest usage: note that an exception will NOT be thrown
            // if there is an error loading this page (see below).

            mWv.loadUrl("http://" + Constants.IP + "/cgi-bin/input");

            mWv.getSettings().setJavaScriptEnabled(true);
            mWv.getSettings().setDomStorageEnabled(true);

            mWv.setWebChromeClient(new WebChromeClient());

            mWv.setWebViewClient(new WebViewClient(){
                public void onPageFinished(WebView mWv, String url) {
                    super.onPageFinished(mWv, url);

                    /**
                     * Pass parametes :
                     * - New SSID
                     * - New Security Key
                     * - Display raw picture
                     */
                    // @"document.getElementsByName('%@')[0].value='%@'"
                    mWv.loadUrl("javascript:document.getElementById('wifi_ssid').value='" +
                                        mEtSSID.getText().toString() + "'");

                    if (mEtSecurityKey.length() == 0
                            | Utils.checkValidatePassword(mEtSecurityKey, 8)) {
                        mWv.loadUrl("javascript:document.getElementById('wifi_key').value='" +
                                            mEtSecurityKey.getText().toString() + "'");

                        if (mCbDisplayRawPicture.isChecked())
                            mWv.loadUrl(
                                    "javascript:document.getElementById('displayraw').value='checked'");
                        else
                            mWv.loadUrl("javascript:document.getElementById('displayraw').value=''");

                        // @"document.forms['sendForm'].submit()"
                        mWv.loadUrl("javascript:document.getElementById('sendForm').submit()");

                        /**
                         * Also get new SSID and new Security Key in Card Setting page
                         */
                        IS_BACK_FROM_CHANGE_SSID_AND_SCCURITY_KEY_TO_CARD_SETTING_PAGE = true;

                        /**
                         * Finish current Change SSID And Security Key after submit success
                         */
                        finish();
                    } else
                        Toast.makeText(SettingAndReviewSettingPage.this,
                                       getString(R.string.toast_your_password_is_case_sensitive),
                                       Toast.LENGTH_SHORT).show();
                }
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

            });

解决方案

当我找到解决方案时,我感到很惊讶。我需要为 Java 脚本代码定义变量。就像下面的代码一样,它起作用了。

mWv.setWebViewClient(new WebViewClient(){
                public void onPageFinished(WebView mWv, String url) {
                    super.onPageFinished(mWv, url);

                    /**
                     * Pass parametes :
                     * - New SSID
                     * - New Security Key
                     * - Display raw picture
                     */
                    // @"document.getElementsByName('%@')[0].value='%@'"

                    mWv.loadUrl("javascript:var x = document.getElementById('wifi_ssid').value = '" +
                                        mEtSSID.getText().toString() + "';");

                    // need check password is correct or not
                    // - empty or minimum has 8 characters.
                    // - have both character & number in typed password
                    if ((mEtSecurityKey.length() == 0
                            | Utils.checkValidatePassword(mEtSecurityKey, 8)
                            & Utils.checkValidatePassword(mEtSecurityKey.getText().toString()))) {
                        mWv.loadUrl("javascript:var y = document.getElementById('wifi_key').value = '" +
                                            mEtSecurityKey.getText().toString() + "';");

                        if (mCbDisplayRawPicture.isChecked())
                            mWv.loadUrl("javascript:var z = document.getElementById('displayraw').value='checked'");
                        else
                            mWv.loadUrl("javascript:var z = document.getElementById('displayraw').value=''");

                        // @"document.forms['sendForm'].submit()"
                        // mWv.loadUrl("javascript:console.log('sendForm: '+document.getElementById('sendForm').value);");
                        mWv.loadUrl("javascript:document.getElementById('sendForm').submit()");

                        /**
                         * Also get new SSID and new Security Key in Card Setting page
                         */
                        IS_BACK_FROM_CHANGE_SSID_AND_SCCURITY_KEY_TO_CARD_SETTING_PAGE = true;

                        /**
                         * Finish current Change SSID And Security Key after submit success
                         */
                        finish();
                    } else
                        Toast.makeText(
                                ChangeSSIDAndPasswordPage.this,
                                getString(R.string.toast_your_password_is_case_sensitive),
                                Toast.LENGTH_SHORT).show();
                }
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

最佳答案

简单的解决方案是确保 WebView 启用了以下设置:

WebView webView = new WebView(this); // this is the context
webView.getSettings().setDomStorageEnabled(true);

这允许浏览器存储页面元素的 DOM 模型,以便 Javascript 可以对其执行操作。

提示:这仅适用于 api 7。

关于javascript - 未捕获的类型错误 : Cannot set property 'value' of > null"in Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27938209/

相关文章:

javascript - 如何强制点击按钮 "General Data"?

javascript - Toast 通知不适用于 JavaScript 调用

android - 同线程情况下如何避免GLSurfaceView出现queueEvent

android - 单击的动画在使用 <shape> 设计后消失了

android - 如何在 Android WebView 渲染之前修改内容?

Android - 在 SurfaceTexture 上绘制 YouTube 视频

javascript - 加载最后 3 个和每个新子项

javascript - 外部javascript中匿名函数之外的变量不起作用

android - LinearLayout 上的选择器颜色

java - Android Webview - 更改输入文本