Android - ListActivity,添加页眉和页脚 View

标签 android header footer listactivity

我正在使用ListActivity、listview。

listView = getListView();

工作完美。我将页脚 View 添加为

LayoutInflater inflater = getLayoutInflater();
listView.addFooterView( inflater.inflate( R.layout.footer, null ), null, false);

一切都 Shiny 但丑陋,所以我想将此页脚 View (仅包含 1 个编辑文本和 1 个按钮)添加到 listView 的标题中

LayoutInflater inflater = getLayoutInflater();
listView.addHeaderView( inflater.inflate( R.layout.footer, null ), null, false);

突然一切都出了问题,我立即得到 RuntimeException。

Suspended(exception RuntimeException)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.access$2200(ActivityThread, Activity$ActiviyRecord, Intent),
so on..

为什么会抛出异常? addFooterView 和 addHeaderView 之间有什么不同,如何将 Header 添加到 ListActivity ?

更新

因此,正如您在评论中看到的那样,我的 logcat 仍然无法工作,但我此时刚刚尝试了下一步:

} catch(Exception e){ 
  Writer result = new StringWriter(); 
  PrintWriter printWriter = new PrintWriter(result);
  e.printStackTrace(printWriter);
  String error = result.toString(); 
}

然后我设置了断点,我可以在表达式部分读取错误。它说 :

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

这对我们所有人都有启发。更改命令类型后,它可以正常工作。

最佳答案

当您登录时

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

Listview的方法addHeaderViewaddFooterView必须在setAdapter之前调用。

关于Android - ListActivity,添加页眉和页脚 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10163196/

相关文章:

仪表板上未报告 Android Flurry 事件

View 上的Android动态气泡

Android 应用程序从后台调用 onCreate()

php - 标题 WordPress 中的登录/注册和货币小部件

css - 页脚不在页面底部

java - 根据对象将对象转换为 JSONObject 或 JSONArray 的方法

Node.js:如何使用请求模块发送带有表单数据的 header ?

html - 在另一个页面上重复页脚 html/css,但显示不同?

仅在 Reportlab 中最后一个数据页上的页脚

c++ - 头文件和附加库