Uncaught TypeError: Unable to set 'value' property of & gt; Null "in Android

advertisements

I try to update information through java script code in Android side.

On Android version 4.3, it's work.

But Android version 4.4, it appeared these error, so can not update information.

[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)

I know from Android version 4.4, already used new Web View to support, but i don't know where is main issue.

People who know how to update information successfully via Java script code even on Android 4.4,

Please tell me,

Thanks you,

p/s : Codes

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

            });

SOLUTION

I surprised when I find out the solution. I need define the variables for Java script codes. Like following code & it worked.

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


The simple solution is to ensure the WebView has the following setting enabled:

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

This allows the browser to store a DOM model of the page elements, so that Javascript can perform operations on it.

Hint: This works only from api 7.