码迷,mamicode.com
首页 > 其他好文 > 详细

addHeaderView()异常 —— setAdapter has already been called

时间:2014-08-13 13:03:16      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:listview   addheaderview   

异常提示:java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.

产生原因:addHeaderView()调用在setAdapter()之后,并且该代码运行在Android4.3之前的系统版本。Android SDKAPI Level对应表

 

我们来看看(Android4.3API-18 和 (Android4.2API-17Listview代码片段:

API-18   

public void addHeaderView(View v, Object data, boolean isSelectable) {
        final FixedViewInfo info = new FixedViewInfo();
        info.view = v;
        info.data = data;
        info.isSelectable = isSelectable;
        mHeaderViewInfos.add(info);
 
        // Wrap the adapter if it wasn't already wrapped.
        if (mAdapter != null) {
            if (!(mAdapter instanceof HeaderViewListAdapter)) {
                mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, mAdapter);
            }
 
            // In the case of re-adding a header view, or adding one later on,
            // we need to notify the observer.
            if (mDataSetObserver != null) {
                mDataSetObserver.onChanged();
            }
        }
}

API-17

public void addHeaderView(View v, Object data, boolean isSelectable) {
 
        if (mAdapter != null && ! (mAdapter instanceof HeaderViewListAdapter)) {
            throw new IllegalStateException(
                    "Cannot add header view to list -- setAdapter has already been called.");
        }
 
        FixedViewInfo info = new FixedViewInfo();
        info.view = v;
        info.data = data;
        info.isSelectable = isSelectable;
        mHeaderViewInfos.add(info);
 
        // in the case of re-adding a header view, or adding one later on,
        // we need to notify the observer
        if (mAdapter != null && mDataSetObserver != null) {
            mDataSetObserver.onChanged();
        }
}


API-17中,假如adapter不为空,则会直接抛出异常,而在API-18中则做了相关的优化。

建议

木有建设性的提议,只能提醒大家,目前市面上主流的Android系统还是Android4.3以下版本,所以在使用addHeaderView()要注意这一点。

 

addHeaderView()异常 —— setAdapter has already been called,布布扣,bubuko.com

addHeaderView()异常 —— setAdapter has already been called

标签:listview   addheaderview   

原文地址:http://blog.csdn.net/mtt1987/article/details/38535249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!