PopupWindow崩溃处理记录
一、Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
java.lang.NullPointerException
Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
崩溃堆栈:
机型统计:
主要发生在Android5.0 5.1的OPPO、VIVO手机上。
源码分析:
Android5.1之前的PopupWindow会直接将contentView add到window上,而之后的PopupWindow则是会将contentView add到一个FrameLayout中再add到window上。
我们的代码中为了获得contentView的尺寸对contentView进行了一次measure,在5.1版本之后的过程中跳过了measure,如果contentView没有指定LayoutParams就会一直为null,之后layout的时候就会抛出异常。
而在5.1之后将contentView add到一个FrameLayout的过程中addView会自动为其增加一个默认的LayoutParams所以不会异常。
解决方案:
对contentView设置LayoutParams。