【安卓】addHeaderView在setAdapter之前还是报错呢?如图
public class MainActivity extends Activity {
private List<Person> mData = null;private Context mContext;
private PersonAdapter mAdapter = null;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
listview = (ListView) findViewById(R.id.listview);
//动态加载顶部View和底部View
final LayoutInflater inflater = LayoutInflater.from(this);
View headView = inflater.inflate(R.layout.listview_header, null, false);
View footView = inflater.inflate(R.layout.listview_footer, null, false);
//添加表头和表尾需要写在setAdapter方法调用之前!!!
Log.e("mAdapter", mAdapter+"");
listview.addHeaderView(headView);
listview.addFooterView(footView);
mData = new LinkedList<Person>();
mData.add(new Person("张三", 20, "男", "四川"));
mData.add(new Person("李四", 10, "男", "广东"));
mData.add(new Person("王五", 44, "女", "广西"));
mData.add(new Person("刘六", 33, "女", "上海"));
mData.add(new Person("汪七", 55, "男", "浙江"));
mAdapter = new PersonAdapter((LinkedList<Person>) mData, mContext);
listview.setAdapter(mAdapter);
}
}
回答
应该没啥大问题,你把build目录删了,重新构建一下。另外,我想看看你的布局。
怀疑listview一直有adapter没有释放
执行addHeaderView时,打log看一下listview.getAdapter()看看是否为空
尝试在onDestory时,执行listview.setAdapter(null)是否有用
以上是 【安卓】addHeaderView在setAdapter之前还是报错呢?如图 的全部内容, 来源链接: utcz.com/a/103307.html