NewbieGuide
NewbieGuide copied to clipboard
遇到的空指针异常getRectF(),源码部分如下
同样遇到类似的问题,那也不应该抛异常吧?看了源码里面,请教个问题,里面抛了异常,是在什么地方捕获的呢? com.app.hubert.guide.util.ViewUtils.getLocationInView(View parent, View child){}
public static Rect getLocationInView(View parent, View child) {
if(child != null && parent != null) { //此处判读了非空情况
View decorView = null;
Context context = child.getContext();
if(context instanceof Activity) {
decorView = ((Activity)context).getWindow().getDecorView();
}
Rect result = new Rect();
Rect tmpRect = new Rect();
View tmp = child;
if(child == parent) {
child.getHitRect(result);
return result;
}
else {
while(tmp != decorView && tmp != parent) {//此处也应该判断非空的情况,捕获该异常,结束后续操作,因为在while循环的时候,此处可能为空了
tmp.getHitRect(tmpRect);
if(!tmp.getClass().equals("NoSaveStateFrameLayout")) {
result.left += tmpRect.left;
result.top += tmpRect.top;
}
tmp = (View)tmp.getParent();
if(tmp != null && tmp.getParent() != null && tmp.getParent() instanceof ViewPager) {
tmp = (View)tmp.getParent();
}
}
result.right = result.left + child.getMeasuredWidth();
result.bottom = result.top + child.getMeasuredHeight();
return result;
}
} else {
throw new IllegalArgumentException("parent and child can not be null .");
}
}`
请大佬关注一下,我直接在依赖中看的源码,却没有发现捕获异常的地方,难道是我多虑了吗?
这里的异常不应该捕获,因为这里抛出异常的情况是:传入的高亮View没有初始化(也就是为null),或者并没有在界面上展示。抛出这种异常表示使用的方式有误,并没有在合适的时机显示引导层,需要调整使用时机。