java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
分析: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.的更多相关文章
- 异常java.lang.IllegalStateException的解决
在初始化viewPagerAdapter时,显示异常.从网上找了找有两类这样的问题,一种是说给一个视图设置了两个父类,如: TextView tv = new TextView();layout.ad ...
- 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 ...
- 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child..."异常处理
这几天做项目用到了ViewPager,因为它可以实现左右划动多个页面的效果,然后 再每个页面里使用ListView,运行时总是出现”PagerAdapter java.lang.IllegalStat ...
- [Android]ListFragment.setEmptyText() 抛 java.lang.IllegalStateException
在ListFragment子类中直接调用setEmptyText(getString(R.string.msg_no_invited_parties)), 抛java.lang.IllegalStat ...
- Bug:java.lang.IllegalStateException
使用迭代的时候,出现了java.lang.IllegalStateException 代码: for ( TaskInfo info : userTaskInfos ) { if ( info.isC ...
- 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme
配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_lau ...
- 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案
在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...
- java.lang.IllegalStateException: Connection pool shut down
最近使用HttpClient 4.5 使用 CloseableHttpClient 发起连接后,使用CloseableHttpResponse 接受返回结果,结果就报错了,上网查了下,有位stacko ...
随机推荐
- visual studio 2013 使用域名调试本地项目
一.在localhost添加需要指定的域名,如:test.domain.com 二.以管理员身份启动VS,并打开项目 三.修改IIS Express的配置文件(一般位于:C:\Users\当前用户\D ...
- Android App的设计架构:MVC,MVP,MVVM与架构经验谈
相关:http://www.cnblogs.com/wytiger/p/5996876.html 和MVC框架模式一样,Model模型处理数据代码不变在Android的App开发中,很多人经常会头疼于 ...
- electron 入门小白贴
electron 入门小白贴 electron demo 跑起来! 毕设准备是做个 跨平台的做题的客户端,打算用 electron 来弄. 然而今天折腾了半天才终于吧demo给跑起来了.经历了许多的问 ...
- Ajax操作如何实现跨域请求 (JSONP和CORS实现Ajax跨域的原理)
由于浏览器存在同源策略机制,同源策略阻止ajax (XMLHttpRequest) 从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. 特别的:由于同源策略是浏览器的限制,所以请求的发送和响 ...
- Scrum meeting 记录
本周Scrum Master 侯宇泰 一. 工作完成内容记录 · 陈双: 1. 后端数据库Azure Storage设计 2. Offline 上传Movie信息 3. 主页和配音页面的连接 · 鲍航 ...
- Word2016(2013)怎么从任意页插入起始页码
添加页码 1 小知识:双击任意页的页脚或页眉区域,激活页眉页脚设计界面.双击文档正文区域,返回正文编辑界面. 2 双击任意页的页脚或页眉区域,激活页眉页脚设计界面.单击"页码" ...
- Request —— 让 Node.js http请求变得超简单
github地址: https://github.com/request/request 安装: npm install request --save-dev
- The commands of Disk
The commands of Disk fdisk( the disk size is less 2TB) fdisk - partition table manipulator for Linux ...
- matlab画图函数plot()/set/legend
简单plot()/legend/XY轴范围axis 除了坐标轴信息外还可以添加其它的信息,如所画曲线的信息等:测试代码如下 x=0:pi/20:2*pi; y1=sin(x); y2=cos(x); ...
- myeclipse环境下开发freemarker
最近在着手一个项目,其中就要使用到freemarker模版,当把html漂亮写完时,改写成freemarker时,问题来了.全屏都是白底黑色,没有高亮显示,没有语法提示,不能格式化.看着眼花,还容易出 ...