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

在Android开发中,当ListView与ScrollView嵌套并尝试动态计算ListView高度时,可能会遇到listItem.measure(0, 0)导致的空指针异常。问题在于非LinearLayout的item布局未重写onMeasure方法。尽管其他布局理论上也重写了此方法,但谷歌源码注释有特别说明,建议使用LinearLayout避免此类问题。" 126180244,12489546,阿里微服务拆分实践:基于DDD的策略与原则,"['微服务', '领域驱动设计(DDD)', '服务拆分', '团队协作', '软件架构']

当我们在使用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);

        }
    }



评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值