我是Android开发人员,也是JavaScript新手。我正在使用javascript控制我的活动的webview。在我的登录页面中,当我使用Java Script函数时,我遇到了一些巨大的错误。请指导我避免这些错误。
代码:
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);");
错误:
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
答案 0 :(得分:1)
根据抛出的错误,似乎document.getElementById(...)
返回null。因此,应用.value
和.innerHTML
来抛出错误。
我怀疑你调用javascript函数时没有加载页面。你什么时候打电话给你的javascript,我的意思是在哪种方法?为确保页面已加载,您可以在loadUrl
对象的onPageFinished
方法中调用WebViewClient
。