您的位置:首页 > 编程语言 > Java开发

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

2015-06-30 15:36 886 查看

异常信息:

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.csdn.net/a443453087/article/details/8079990
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: