Uncaught TypeError: Cannot set property 'value' of > null" in Android

Uncaught TypeError: Cannot set property 'value' of > null" in Android

我尝试通过java script code in Android side更新信息。

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

但是Android4.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支持,但我不知道主要问题在哪里。

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

请告诉我,

谢谢你,

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.

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

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;
                }
            });

简单的解决方案:

 wv.loadUrl("javascript:document.getElementById('xx').value='';void(0);");

为了防止浏览器重定向到 JavaScript 评估结果的纯文本版本,将 JavaScript 字符串附加 void(0);

参考:void运算符