简体   繁体   中英

Uncaught TypeError when calling a Javascript Function in Android

I am an Android developer and new to JavaScript. I am controlling the webview from my activity using javascript. In my login page when I use the Java Script functions I getting some huge errors. Please guide me to avoid these errors.

Code:

web.loadUrl("javascript:document.getElementById(\"umail\").value = \""
        + email + "\";");
web.loadUrl("javascript:document.getElementById(\"password\").value = \""
        + pwd + "\";");
web.loadUrl("javascript:document.getElementById(\"confirmpassword\").value = \""
        + confirm_pwd + "\";");
web.loadUrl("javascript:func_register_step1('signin')");

web.loadUrl("javascript:window.JSInterface.getErrorMSG(document.getElementById('errormessagetd').innerHTML);");
web.loadUrl("javascript:window.JSInterface.getWarningMSG(document.getElementById('warning_msg').innerHTML);");

Error:

05-15 11:52:28.310: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:28.310: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:28.310: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:28.310: E/Web Console(3722): Uncaught ReferenceError: func_register_step1 is not defined at null:1
05-15 11:52:28.310: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:28.310: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:31.240: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:31.240: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:31.240: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:31.240: E/Web Console(3722): Uncaught ReferenceError: func_register_step1 is not defined at null:1
05-15 11:52:31.250: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:31.250: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:33.590: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:33.590: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:33.590: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:33.590: E/Web Console(3722): Uncaught ReferenceError: func_register_step1 is not defined at null:1
05-15 11:52:33.600: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:33.600: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:40.260: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:40.270: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:40.270: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:40.270: E/Web Console(3722): Uncaught ReferenceError: func_register_step1 is not defined at null:1
05-15 11:52:40.270: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:40.270: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:41.290: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:41.290: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:41.290: E/Web Console(3722): Uncaught TypeError: Cannot set property 'value' of null at null:1
05-15 11:52:41.290: E/Web Console(3722): Uncaught ReferenceError: func_register_step1 is not defined at null:1
05-15 11:52:41.290: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1
05-15 11:52:41.290: E/Web Console(3722): Uncaught TypeError: Cannot read property 'innerHTML' of null at null:1

According to the thrown errors, it seems that document.getElementById(...) returns null. And so applying .value and .innerHTML to null throw the errors.

I suspect the page is not loaded when you call your javascript function. When do you call your javascript, I mean in which method? To be sure the page is loaded, you can call loadUrl in the onPageFinished method of the WebViewClient object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM