天天看點

TypeError:Cannot call method 'getItem' of null

     這個錯誤是我在android中使用webview打開一個帶有發送短信的html5頁面,發送短信報的錯誤,發送短信我使用的是leancloud提供了一套手機短信驗證

<span style="font-size:14px;">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("驗證碼擷取失敗,請稍後再試!");
             }
         });</span>
           

然而在調用這個方法的時候一直報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);      
<span style="font-size:14px;"> WebSettings settings = webView.getSettings();
 settings.setDomStorageEnabled(true);</span>
           

這樣問題就解決了,又死掉了好多腦細胞!!!!