Listview item.measure(0, 0);报空指针时问题

本文解决了一个在Android 4.2及以下版本中ListView截图时出现的空指针异常问题。介绍了错误的原因在于RelativeLayout测量子视图时可能出现的问题,并提供了三种解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做了一个截取ListView内容保存为图片的功能,测试的时候发现4.2的模拟器上报错,查看一番之后发现是计算item时候报空指针,查到的解决方案如下:

转载地址:解决方案

当调用listItem.measure(0, 0);报空指针时问题:
检查Adapter适配时Item的根容器为RelativeLayout,
报错原因:
In platform version 17 and lower, RelativeLayout was affected by a measurement bug that could cause child views to be measured with incorrect MeasureSpec values. (See MeasureSpec.makeMeasureSpec for more details.) This was triggered when a RelativeLayout container was placed in a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view not equipped to properly measure with the MeasureSpec mode UNSPECIFIED was placed in a RelativeLayout, this would silently work anyway as RelativeLayout would pass a very large AT_MOST MeasureSpec instead.
This behavior has been preserved for apps that set android:targetSdkVersion=”17” or older in their manifest’s uses-sdktag for compatibility. Apps targeting SDK version 18 or newer will receive the correct behavior
有三种解决方案:
一、升级版本到4.2.2
二、更改根容器为LinearLayout
三、在适配器里添加convertView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); convertView为Item的view

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值