WebView显示网页出现Uncaught TypeError: Cannot call method 'getItem' of null异常

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的本地存储功能。

  1. webview.getSettings().setDomStorageEnabled(true);
  2. webview.getSettings().setAppCacheMaxSize(1024*1024*8);
  3. String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
  4. webview.getSettings().setAppCachePath(appCachePath);
  5. webview.getSettings().setAllowFileAccess(true);
  6. webview.getSettings().setAppCacheEnabled(true);


其中第二行是设置大小,新的api是不用设置的,会知道增长,已经过期的方法,保持兼容,也可以加上


				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值