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