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 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

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 记录