分析:android 4.2.X及以下的版本,addHeaderView必须在setAdapter之前,否则会抛出IllegalStateException。

android 4.2.X(API 17) ListView源码如下:

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();
}
}

  

android 4.3.X(API 18)ListView源码如下:

    public void addHeaderView(View v, Object data, boolean isSelectable) {
...... // 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();
}
}
}

所以为了兼容android4.2.x及以下的版本,最好先addHeaderView,后setAdapter.

java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.的更多相关文章

  1. 异常java.lang.IllegalStateException的解决

    在初始化viewPagerAdapter时,显示异常.从网上找了找有两类这样的问题,一种是说给一个视图设置了两个父类,如: TextView tv = new TextView();layout.ad ...

  2. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    在ViewPager中,用Fragment显示页面时,报错: java.lang.IllegalStateException: The specified child already has a pa ...

  3. 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child..."异常处理

    这几天做项目用到了ViewPager,因为它可以实现左右划动多个页面的效果,然后 再每个页面里使用ListView,运行时总是出现”PagerAdapter java.lang.IllegalStat ...

  4. [Android]ListFragment.setEmptyText() 抛 java.lang.IllegalStateException

    在ListFragment子类中直接调用setEmptyText(getString(R.string.msg_no_invited_parties)), 抛java.lang.IllegalStat ...

  5. Bug:java.lang.IllegalStateException

    使用迭代的时候,出现了java.lang.IllegalStateException 代码: for ( TaskInfo info : userTaskInfos ) { if ( info.isC ...

  6. 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题

    原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...

  7. java.lang.IllegalStateException: You need to use a Theme.AppCompat theme

    配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_lau ...

  8. 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案

    在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...

  9. java.lang.IllegalStateException: Connection pool shut down

    最近使用HttpClient 4.5 使用 CloseableHttpClient 发起连接后,使用CloseableHttpResponse 接受返回结果,结果就报错了,上网查了下,有位stacko ...

随机推荐

  1. 个人理解c#对称加密 非对称加密 散列算法的应用场景

    c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...

  2. Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  3. dietpi请暂时不要升级为jessie

    关于升级到Debian最新稳定版jessie,作者是这样说的:一旦官方Raspbian升级到Jessie,DietPi也会随之切换到Jessie.由于改动较大,很多功能需要修改后才能正常使用,到时候可 ...

  4. coreseek 安装及使用方法详解

    coreseek 安装及使用 一般站点都需要搜索功能,如果是php+mysql站点,建议选择coreseek,如果是java站点建议使用lucene,coreseek 是一款很好的中文全文检索/搜索软 ...

  5. 【原创】PageAdminCMS 前台SQL注入漏洞(2)

    之前根据公司的要求找了几个web程序的漏洞提交CNVVD,发现漏洞提交上去两个月了,CNVVD却没有任何回应,我提交的这几个漏洞却悄悄的修补掉了. 文章作者:rebeyond 受影响版本:V3.0 漏 ...

  6. 记一次特别的bug

    问题现象 push入某个特定的画页,然后再pop出来,然后再切换不同的tabbaritem就会崩溃,而且没有任何提示,也没法定位崩溃位置,哪怕用了$arg1. 猜测,这种情况坏访问的可能性比较大,至少 ...

  7. todo

     ID生成器要做成兼容分布式, 数据库ef控制要改成手动升级, 异常日志模块,操作日志某快,,, 日志服务器,, 图片服务器,,,动静分离, 前后台分离,, 可扩展性,无状态化集群弹性部署, 数据库主 ...

  8. Sybase 常用SQL

    http://blog.chinaunix.net/uid-200142-id-3073894.html ===========================使用总结================ ...

  9. SQL批量更新 关系表更新

    很多人在做数据的批量更新时..如果更新的内容是从其他表查出来的..很容易这么写.. UPDATE TABLE1 SET COLUMN1=(SELECT SUM(SOMETHING) FROM TABL ...

  10. mysql-开启慢查询&所有操作记录日志

    在运营网站的过程中,可能会遇到网站突然变慢的问题,一般情况下和 MySQL 慢有关系,可以通过开启慢查询,找到影响效率的 SQL ,然后采取相应的措施.下面介绍一下如何开启慢查询: 1.开启慢查询 找 ...