动态计算listview的高度listItem.measure(0, 0)报空指针异常解决办法
来源:互联网 发布:mac版的仙剑没有文字 编辑:程序博客网 时间:2024/05/21 03:16
当我们在使用Listview这类的控件和scrollview嵌套使用的时候会调用一个动态计算listview高度的方法。网上大部分方法基本都如下:
public class Utils { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } }
但是有时候我们会发现每次调用的时候都会产生在listItem.measure(0,0)报空指针异常。但是你去debug会发现listItem 并不是为空,那么为啥会报错呢。你去看你的item的布局会发现肯定不是LinearLayout,这个时候把它换为LinearLayout 就好了。网上的说法是原来是 Linearlayout重写了onmeasure方法,其他的布局文件没有重写onmeasure,所以在调用listItem.measure(0, 0); 会报空指针异常,如果想用这个东东,就必须用linearlayout布局。但是我看源码发现其实并不能这样说,其他布局也是重写了onmeasure方法的,不过谷歌有特别备注说明:
// We need to know our size for doing the correct computation of children positioning in RTL // mode but there is no practical way to get it instead of running the code below. // So, instead of running the code twice, we just set the width to a "default display width" // before the computation and then, as a last pass, we will update their real position with // an offset equals to "DEFAULT_WIDTH - width".
大伙去翻译翻译看看啥意思。而且这种情况并不是在所有的手机上会出现,现在很多手机厂商有修改rom。我只是在华为的一款手机上报了这个错。基于这些我写了一个稳妥一点的计算方法:
public class Utils { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getRefreshableView().getAdapter();if (listAdapter == null) { return;}int totalHeight = 0;for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem == null) continue; if (listItem instanceof LinearLayout){ listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); }else { try { listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); }catch (NullPointerException e){ totalHeight += DeviceUtil.dp_to_px(ClientApplication.instance,80);//这里自己随便写个大小做容错处理吧 LogOut.e("bobge","NullPointerException"); } } totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight + (listView.getRefreshableView().getDividerHeight() * (listAdapter.getCount() - 1));listView.setLayoutParams(params); } }
0 0
- 动态计算listview的高度listItem.measure(0, 0)报空指针异常解决办法
- 动态计算listview的高度listItem.measure(0, 0)报空指针异常解决办法
- ScrollView嵌套ListView,listItem.measure(0,0);报空指针异常NullPointerException
- Scrollview嵌套listView出现listItem.measure(0, 0)报空指针异常原因
- 解决ListView 和ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 和ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- Listview item.measure(0, 0);报空指针时问题
- ScrollView嵌套ListView,.measure(0,0);空指针NullException
- Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0, 0))
- Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0, 0))
- 【Android】自定义ListView的Adapter报空指针异常解决方法
- 【转】Android 使用ViewPager实现类似gallery画廊的效果(画廊效果之ViewPager显示多个图片)
- 欢迎使用CSDN-markdown编辑器
- Timer(TimerTask )\AlarmManger\Handler\Thread.sleep\ScheduledThreadPoolExecutor
- 102. 107.Binary Tree Level Order Traversal
- ssm注解方式实现启动tomcat报错
- 动态计算listview的高度listItem.measure(0, 0)报空指针异常解决办法
- LeetCode OJ-48-Rotate Image
- 如何获取文件后缀名(无后缀名或后缀名被修改--非常规需求)
- iOS 线程延迟
- android-ScrollView嵌套ListView冲突解决办法
- Unity Awake函数的小问题
- 技术人员如何创业《一》- 产品及想法
- Windows安装Resin配置详解与发布应用教程
- Docker监测:cAdvisor 的构筑方法