美文网首页
java.lang.IllegalArgumentExcepti

java.lang.IllegalArgumentExcepti

作者: 2ece9f02c806 | 来源:发表于2016-07-24 10:18 被阅读599次

复现:

当ListView的item中有Edittext,并且已经调起了软键盘的时候,去滑动ListView会出现该异常导致APP crash

原因:

发生这个错误主要是ListView或者其它ViewGroup等容器控件因为滑动而暂时移除子View,但却没有移除该子View上面的焦点Focus,所以在ListView滑动返回到原来的位置的时候没有恢复成原来的View,导致了该异常的产生

解决:

在开始滑动的时候清除当前的焦点就可以了

public class CleanFocusScrollListener implements AbsListView.OnScrollListener {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {

            View currentFocus = null;
            if (view.getContext() instanceof Activity) {
                currentFocus = ((Activity) view.getContext()).getCurrentFocus();
            }
            if (currentFocus != null) {
                currentFocus.clearFocus();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}

相关文章

网友评论

      本文标题:java.lang.IllegalArgumentExcepti

      本文链接:https://www.haomeiwen.com/subject/ebgwjttx.html