java.lang.IllegalArgumentException: parameter must be a descendant of this view

一,问题描述

ScrollView 里面嵌套了 RecyclerView,当RecyclerView里面的EditText获取焦点,然后更新数据,会闪退,并报如下错误:

java.lang.IllegalArgumentException: parameter must be a descendant of this view

二,原因分析

根据堆栈信息,界面刷新时,调用ScrollView的onSizeChanged()方法,在onSizeChanged()方法中调用了findfocus方法,得到返回的界面中当前拥有焦点的view。然后将该View作为参数调用ViewGroup的方法 offsetRectBetweenParentAndChild,在执行下面代码时抛出异常,app闪退。

上面代码中的this指的是ScrollView,说明在onSizeChange中获取的拥有焦点的View不是ScrollView的子View,至于为什么这样,要结合你的布局分析。

我也没有找到原因,部分手机正常,少数几部会出现这个问题。

解决方式是自定义ScrollView,重写isFoused方法。代码如下:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class FocusedScrollView extends ScrollView {
    public FocusedScrollView(Context context) {
        super(context);
    }

    public FocusedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

暂不知道有没有什么后遗症。

发布了87 篇原创文章 · 获赞 14 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/epitomizelu/article/details/103597613