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

本文介绍了解决 ListView 在滚动过程中因焦点未正确处理而导致的 IllegalArgumentException 异常方法。通过实现 OnScrollListener,在特定状态下清除子视图焦点,避免错误发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常信息:

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

错误原因:

发生这个错误主要是ListView或者其它ViewGroup等容器控件因为滑动而暂时移除子View,但却没有移除该子View上面的焦点Focus,所以在ListView滑动返回到原来的位置的时候没有恢复成原来的View,导致了该异常的产生,具体的源码分析可以查看参考链接[2]

解决方法:

解决办法就是清除掉ListView中item的焦点即可,下面是来自参考链接[1]的解决问题的代码:

protected class MyScrollListener implements OnScrollListener {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // do nothing 
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
                View currentFocus = getCurrentFocus();
                if (currentFocus != null) {
                    currentFocus.clearFocus();
                }
            }
        }

    }

这段代码是给ListView设置ScrollListener监听,使得ListView在滚动的时候可以清除掉ChildView中获取的焦点,大家也可以参考下参考链接[1]中其他的解决方法,我的代码中出现这个问题是因为我的ListView中inflate了一个HeaderView,这个HeaderView中有一个EditText输入框,并且这个EditText获取到了焦点,所以我的ListView在向下滚动然后再返回到ListView顶部的时候会出现这个异常,我的解决办法就比较简单了,直接移除HeaderView中EditText的焦点就行了(mEditText.clearFocus();)。

参考链接:

http://stackoverflow.com/questions/7100555/preventing-catching-illegalargumentexception-parameter-must-be-a-descendant-of
http://blog.sina.com.cn/s/blog_5704bfaf0102v3bn.html
http://www.cnblogs.com/LIANQQ/p/4026560.html
http://www.educity.cn/wenda/100840.html
http://blog.youkuaiyun.com/a443453087/article/details/8079990

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值