gpt4 book ai didi

javascript - 在 Android 中调用 Javascript 函数时未捕获的类型错误

转载 作者:行者123 更新时间:2023-11-30 03:36:45 25 4
gpt4 key购买 nike

我是一名 Android 开发者,刚接触 JavaScript。我正在使用 javascript 从我的 Activity 中控制 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

最佳答案

根据抛出的错误,似乎 document.getElementById(...) 返回 null。因此将 .value.innerHTML 应用于 null 会抛出错误。

我怀疑当您调用 javascript 函数时页面未加载。你什么时候调用你的javascript,我的意思是用哪种方法?为确保页面已加载,您可以在 WebViewClient 对象的 onPageFinished 方法中调用 loadUrl

关于javascript - 在 Android 中调用 Javascript 函数时未捕获的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16558357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com