未捕获的TypeError:无法设置属性' value' >空"在Android中

时间:2015-01-14 07:58:15

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

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

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

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

  

[INFO:CONSOLE(1)]“Uncaught TypeError:无法设置属性'value'   null“,source:(1)I / chromium:[INFO:CONSOLE(1)]”Uncaught TypeError:   无法设置null的属性'value',来源:(1)I / chromium:   [INFO:CONSOLE(1)]“Uncaught TypeError:无法调用方法'submit'   null“,来源:(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;
                }
            });

3 个答案:

答案 0 :(得分:3)

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

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

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

提示:这仅适用于api 7.

答案 1 :(得分:3)

当我找到解决方案时,我感到很惊讶。我需要为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;
                }
            });

答案 2 :(得分:0)

简单的解决方案:

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

要防止浏览器重定向到评估JavaScript附加void(0);

的JavaScript字符串的结果的纯文本版本

参考:void运算符