动态计算listview的高度listItem.measure(0, 0)报空指针异常解决办法

当我们在使用Listview这类的控件和scrollview嵌套使用的时候会调用一个动态计算listview高度的方法。网上大部分方法基本都如下:

[java]  view plain  copy
  1. public class Utils {  
  2.         public static void setListViewHeightBasedOnChildren(ListView listView) {  
  3.             ListAdapter listAdapter = listView.getAdapter();   
  4.             if (listAdapter == null) {  
  5.                 // pre-condition  
  6.                 return;  
  7.             }  
  8.   
  9.             int totalHeight = 0;  
  10.             for (int i = 0; i < listAdapter.getCount(); i++) {  
  11.                 View listItem = listAdapter.getView(i, null, listView);  
  12.                 listItem.measure(00);  
  13.                 totalHeight += listItem.getMeasuredHeight();  
  14.             }  
  15.   
  16.             ViewGroup.LayoutParams params = listView.getLayoutParams();  
  17.             params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  18.             listView.setLayoutParams(params);  
  19.         }  
  20.     }  

但是有时候我们会发现每次调用的时候都会产生在listItem.measure(0,0)报空指针异常。但是你去debug会发现listItem 并不是为空,那么为啥会报错呢。你去看你的item的布局会发现肯定不是LinearLayout,这个时候把它换为LinearLayout 就好了。网上的说法是原来是 Linearlayout重写了onmeasure方法,其他的布局文件没有重写onmeasure,所以在调用listItem.measure(0, 0); 会报空指针异常,如果想用这个东东,就必须用linearlayout布局。但是我看源码发现其实并不能这样说,其他布局也是重写了onmeasure方法的,不过谷歌有特别备注说明:

[java]  view plain  copy
  1. // We need to know our size for doing the correct computation of children positioning in RTL  
  2.        // mode but there is no practical way to get it instead of running the code below.  
  3.        // So, instead of running the code twice, we just set the width to a "default display width"  
  4.        // before the computation and then, as a last pass, we will update their real position with  
  5.        // an offset equals to "DEFAULT_WIDTH - width".  

大伙去翻译翻译看看啥意思。而且这种情况并不是在所有的手机上会出现,现在很多手机厂商有修改rom。我只是在华为的一款手机上报了这个错。基于这些我写了一个稳妥一点的计算方法:

[java]  view plain  copy
  1. public class Utils {  
  2.         public static void setListViewHeightBasedOnChildren(ListView listView) {  
  3.              
  4. ListAdapter listAdapter = listView.getRefreshableView().getAdapter();  
  5. if (listAdapter == null) {  
  6.     return;  
  7. }  
  8.   
  9. int totalHeight = 0;  
  10.   
  11. for (int i = 0; i < listAdapter.getCount(); i++) {  
  12.   
  13.     View listItem = listAdapter.getView(i, null, listView);  
  14.     if (listItem == nullcontinue;  
  15.   
  16.     if (listItem instanceof LinearLayout){  
  17.         listItem.measure(00);  
  18.         totalHeight += listItem.getMeasuredHeight();  
  19.     }else {  
  20.         try {  
  21.             listItem.measure(00);  
  22.             totalHeight += listItem.getMeasuredHeight();  
  23.         }catch (NullPointerException e){  
  24.             totalHeight += DeviceUtil.dp_to_px(ClientApplication.instance,80);//这里自己随便写个大小做容错处理吧  
  25.             LogOut.e("bobge","NullPointerException");  
  26.         }  
  27.     }  
  28.   
  29.     totalHeight += listItem.getMeasuredHeight();  
  30.   
  31. }  
  32.   
  33. ViewGroup.LayoutParams params = listView.getLayoutParams();  
  34.   
  35. params.height = totalHeight  
  36.         + (listView.getRefreshableView().getDividerHeight() * (listAdapter.getCount() - 1));  
  37.   
  38.   
  39. listView.setLayoutParams(params);  
  40.   
  41.         }  
  42.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值