WebView显示网页出现Uncaught TypeError: Cannot call method 'getItem' of null异常
09-21 16:17:49.161: I/chromium(18581): [INFO:CONSOLE(8)] "Uncaught TypeError: Cannot call method 'getItem' of null", source:.....
出现异常情况,项目中使用webview来显示网页,现在很多webapp都内嵌网页,所以我们webview需要启动js等东西。
上面这个异常,出现什么getItem of null ,我查看了一下网页的js
var tempLang = window.localStorage.getItem("globalLang");
这个行出现了异常,这个正是html5的特性,一个本地存储的东西,存储量比cookie大,但是这个必须在android的webview用代码启动才行
如果找到js错误,一是都难找到这个解决方法,估计自己还对webview没有深入了解吧,过段时间弄个webview整体学习笔记,总结一下
解决方法:
启动webview的html5的本地存储功能。
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAppCacheMaxSize(1024*1024*8);
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
webview.getSettings().setAppCachePath(appCachePath);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
其中第二行是设置大小,新的api是不用设置的,会知道增长,已经过期的方法,保持兼容,也可以加上