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

来源:互联网 发布:电音和摇滚知乎 编辑:程序博客网 时间:2024/06/03 15:32

当我们在使用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
原创粉丝点击