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

3 篇文章 0 订阅
3 篇文章 0 订阅

异常信息

大致的异常信息如下:

java.lang.IllegalArgumentException: parameter must be a descendant of this view
 E/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 parent
        while ((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 space
        if (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 = position
          		mAddImageHelper.selectImageSingle(this,REQUEST_ADD_GOODS_TYPE_IMAGE)
}

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

android:windowSoftInputMode="stateHidden|adjustPan"

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JavPer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值