TypeError:Cannot call method 'getItem' of null
16lz
2021-01-26
这个错误是我在android中使用webview打开一个带有发送短信的html5页面,发送短信报的错误,发送短信我使用的是leancloud提供了一套手机短信验证
AV.Cloud.requestSmsCode({ mobilePhoneNumber : mb, name : 'XXXX, op : '手机绑定验证', ttl : 2 }).then(function() { //发送成功 getCode(document.getElementById("getCheckCode")); }, function(err) { if(err.code==127){ alert("请输入正确的手机号码!"); }else if(err.code==601){ alert("发送短信过于频繁,请稍后再试!"); }else{ //发送失败 alert("验证码获取失败,请稍后再试!"); } });
然而在调用这个方法的时候一直报TypeError:Cannot call method 'getItem' of null,但是在PC端测试是没有问题的,
然后我找到那行错误的地方,localStorage.getItem,是使用Dom的本地存储机制,那么可能最大的问题就是在webview没开启DOM storage的问题,果然经过多方查证,原来这个默认是不开启DOM storage,导致后面在使用的时候找不到而报错。
/** * Sets whether the DOM storage API is enabled. The default value is false. * * @param flag true if the WebView should use the DOM storage API */public abstract void setDomStorageEnabled(boolean flag);
WebSettings settings = webView.getSettings(); settings.setDomStorageEnabled(true);
这样问题就解决了,又死掉了好多脑细胞!!!!
更多相关文章
- AndroidStudio部署项目时出现错误:Instant Run requires 'Tools |
- android 跳转时出现短暂的黑屏继承AppCompatActivity问题
- ubuntu13.10编译android文件系统4.0.4错误全部解析
- android访问静态页面,出现405错误解决方法
- android 点击获取验证码显示倒计时并不可用
- android之Can't create handler inside thread that has not cal
- Android使用正则表达式验证手机号
- tabhost (activitygroup)中子页面弹出对话框
- Android(安卓)- 向服务器发送数据(GET).