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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/qjsjp/article/details/109262503
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/704865

相关文章

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 模块理论

详解Python中通用工具类与异常处理

《详解Python中通用工具类与异常处理》在Python开发中,编写可重用的工具类和通用的异常处理机制是提高代码质量和开发效率的关键,本文将介绍如何将特定的异常类改写为更通用的ValidationEx... 目录1. 通用异常类:ValidationException2. 通用工具类:Utils3. 示例文

无人叉车3d激光slam多房间建图定位异常处理方案-墙体画线地图切分方案

墙体画线地图切分方案 针对问题:墙体两侧特征混淆误匹配,导致建图和定位偏差,表现为过门跳变、外月台走歪等 ·解决思路:预期的根治方案IGICP需要较长时间完成上线,先使用切分地图的工程化方案,即墙体两侧切分为不同地图,在某一侧只使用该侧地图进行定位 方案思路 切分原理:切分地图基于关键帧位置,而非点云。 理论基础:光照是直线的,一帧点云必定只能照射到墙的一侧,无法同时照到两侧实践考虑:关

Thymeleaf:生成静态文件及异常处理java.lang.NoClassDefFoundError: ognl/PropertyAccessor

我们需要引入包: <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>sp

深入理解数据库的 4NF:多值依赖与消除数据异常

在数据库设计中, "范式" 是一个常常被提到的重要概念。许多初学者在学习数据库设计时,经常听到第一范式(1NF)、第二范式(2NF)、第三范式(3NF)以及 BCNF(Boyce-Codd范式)。这些范式都旨在通过消除数据冗余和异常来优化数据库结构。然而,当我们谈到 4NF(第四范式)时,事情变得更加复杂。本文将带你深入了解 多值依赖 和 4NF,帮助你在数据库设计中消除更高级别的异常。 什么是