NestedScrollView引起的ViewGroup 异常: parameter must be a descendant of this view

本文主要是介绍NestedScrollView引起的ViewGroup 异常: parameter must be a descendant of this view,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

异常信息

大致的异常信息如下:

java.lang.IllegalArgumentException: parameter must be a descendant of this viewE/DEBUG:     at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:6376)at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:6305)at androidx.core.widget.NestedScrollView.isWithinDeltaOfScreen(NestedScrollView.java:1387)at androidx.core.widget.NestedScrollView.onSizeChanged(NestedScrollView.java:1872)at android.view.View.sizeChange(View.java:22338)at android.view.View.setFrame(View.java:22290)at android.view.View.layout(View.java:22144)at android.view.ViewGroup.layout(ViewGroup.java:6493)at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)

异常发生时机

界面的大概视图布局NestedScrollView中嵌套了几个RecyclerView,还有普通的视图,其中一个RecyclerView 主要做商品类型的展现,需要用户手动点击添加,如下列图:
在这里插入图片描述
在这里插入图片描述
点击类型后面的EditText输入内容以后,再点击旁边的图标选择相册图片,然后返回构建视图时,界面闪退。

异常定位分析

看异常信息分析,应该是我视图布局中的NestedScrollView 在onSizeChanged的时候调用isWithinDeltaOfScreen方法,在调用ViewGroup的offsetDescendantRectToMyCoords方法,最终在offsetRectBetweenParentAndChild方法中抛出异常:

 protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);View currentFocused = findFocus();if (null == currentFocused || this == currentFocused) {return;}// If the currently-focused view was visible on the screen when the// screen was at the old height, then scroll the screen to make that// view visible with the new screen height.if (isWithinDeltaOfScreen(currentFocused, 0, oldh)) {currentFocused.getDrawingRect(mTempRect);offsetDescendantRectToMyCoords(currentFocused, mTempRect);int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);doScrollY(scrollDelta);}}
void offsetRectBetweenParentAndChild(View descendant, Rect rect,boolean offsetFromChildToParent, boolean clipToBounds) {// already in the same coord system :)if (descendant == this) {return;}ViewParent theParent = descendant.mParent;// search and offset up to the parentwhile ((theParent != null)&& (theParent instanceof View)&& (theParent != this)) {if (offsetFromChildToParent) {rect.offset(descendant.mLeft - descendant.mScrollX,descendant.mTop - descendant.mScrollY);if (clipToBounds) {View p = (View) theParent;boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,p.mBottom - p.mTop);if (!intersected) {rect.setEmpty();}}} else {if (clipToBounds) {View p = (View) theParent;boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,p.mBottom - p.mTop);if (!intersected) {rect.setEmpty();}}rect.offset(descendant.mScrollX - descendant.mLeft,descendant.mScrollY - descendant.mTop);}descendant = (View) theParent;theParent = descendant.mParent;}// now that we are up to this view, need to offset one more time// to get into our coordinate spaceif (theParent == this) {if (offsetFromChildToParent) {rect.offset(descendant.mLeft - descendant.mScrollX,descendant.mTop - descendant.mScrollY);} else {rect.offset(descendant.mScrollX - descendant.mLeft,descendant.mScrollY - descendant.mTop);}} else {throw new IllegalArgumentException("parameter must be a descendant of this view");}}

看视图看异常信息,怎么也想不到 参数为啥不是此视图的子视图,后来追源码,看onSizeChanged里View currentFocused = findFocus() 这行代码,突然想到了当前的输入法软键盘,然后就怀疑是输入法键盘的问题,于是我做了一个测试,输入文字信息以后,手动收回输入法键盘,再去点击旁边的小图标选择相册,果然返回正常的。
那么异常原因应该可以得到定位了,估计是因为输入法软键盘弹起,在NestedScrollView 的onSizeChanged方法回调时,输入法键盘获取到焦点,最终导致这个异常的发生。

异常解决方案

两个方案:
1、界面跳转选择相册时,先隐藏掉输入法,这样返回来构建界面时就不会抛这个异常了,比如下面代码实现:

R.id.goodsTypeImageIv ->{// 跳转之前,将输入法隐藏掉ApplictionUtil.hidenInputMethod(goodsTypeRecyclerView, this)mGoodsTypeCurrentIndex = positionmAddImageHelper.selectImageSingle(this,REQUEST_ADD_GOODS_TYPE_IMAGE)
}

2、还有一个思路,就是在输入法软键盘弹出时,不对界面window的大小进行调整,配置activity的windowSoftInputMode属性为adjustPan即可

android:windowSoftInputMode="stateHidden|adjustPan"

配置adjustPan有可能会造成界面显示不友好,如果必须要用adjustResize,那么还是使用第一个解决方案,点击跳转前,先隐藏掉输入法。

这篇关于NestedScrollView引起的ViewGroup 异常: parameter must be a descendant of this view的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/704865

相关文章

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

Java异常架构Exception(异常)详解

《Java异常架构Exception(异常)详解》:本文主要介绍Java异常架构Exception(异常),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. Exception 类的概述Exception的分类2. 受检异常(Checked Exception)

Java报NoClassDefFoundError异常的原因及解决

《Java报NoClassDefFoundError异常的原因及解决》在Java开发过程中,java.lang.NoClassDefFoundError是一个令人头疼的运行时错误,本文将深入探讨这一问... 目录一、问题分析二、报错原因三、解决思路四、常见场景及原因五、深入解决思路六、预http://www

一文带你深入了解Python中的GeneratorExit异常处理

《一文带你深入了解Python中的GeneratorExit异常处理》GeneratorExit是Python内置的异常,当生成器或协程被强制关闭时,Python解释器会向其发送这个异常,下面我们来看... 目录GeneratorExit:协程世界的死亡通知书什么是GeneratorExit实际中的问题案例

Java捕获ThreadPoolExecutor内部线程异常的四种方法

《Java捕获ThreadPoolExecutor内部线程异常的四种方法》这篇文章主要为大家详细介绍了Java捕获ThreadPoolExecutor内部线程异常的四种方法,文中的示例代码讲解详细,感... 目录方案 1方案 2方案 3方案 4结论方案 1使用 execute + try-catch 记录

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

Python中异常类型ValueError使用方法与场景

《Python中异常类型ValueError使用方法与场景》:本文主要介绍Python中的ValueError异常类型,它在处理不合适的值时抛出,并提供如何有效使用ValueError的建议,文中... 目录前言什么是 ValueError?什么时候会用到 ValueError?场景 1: 转换数据类型场景

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结

Python中的异步:async 和 await以及操作中的事件循环、回调和异常

《Python中的异步:async和await以及操作中的事件循环、回调和异常》在现代编程中,异步操作在处理I/O密集型任务时,可以显著提高程序的性能和响应速度,Python提供了asyn... 目录引言什么是异步操作?python 中的异步编程基础async 和 await 关键字asyncio 模块理论