【问题标题】:Android - ListActivity, add Header and Footer viewAndroid - ListActivity,添加 Header 和 Footer 视图
【发布时间】:2012-04-27 03:18:27
【问题描述】:

我正在使用 ListActivity,listview。

listView = getListView();

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

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

一切都很闪亮但丑陋,所以我想将此页脚视图(仅包含 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 中?

更新

所以你可以在 cmets 中阅读,我的 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. 

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

【问题讨论】:

  • @add full logcat here...
  • logcat 根本不工作:S 从来没有对我工作过
  • @Victor:“logcat 根本不工作:S 从来没有对我工作过” - 你的意思是你不知道如何访问 logcat 数据?启动任何模拟器或将手机连接到 PC 并在 eclipse 中使用 DDMS,您会看到正在运行的每个应用程序都会将数据输出到 logcat。
  • @MisterSquonk 我的意思是,logcat 没有报告任何内容
  • @Victor: 然后用catch(Exception e) 捕获异常并在catch 块中使用e.printStackTrace()

标签: android header footer listactivity


【解决方案1】:

登录时

java.lang.IllegalStateException: 无法将标题视图添加到列表 -- setAdapter 已被调用。

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

【讨论】:

  • 是的,我也想添加答案,但由于我的声誉低,我必须等待 8 小时才能回答我自己的问题。
【解决方案2】:

为页眉和页脚创建布局 xml

header_layout.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="header"
    />
    </RelativeLayout>

footer_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="footer"
/>
</RelativeLayout>

现在在活动 java 文件中添加 onCreate() 方法

listView = (ListView) findViewById(R.id.listView1);
LayoutInflater inflater = getLayoutInflater();
    ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
            false);
    ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
            false);
    listView.addHeaderView(header, null, false);
    listView.addFooterView(footer, null, false);
    listView.setAdapter(adapter);

【讨论】:

    【解决方案3】:

    所以如果你想在你的 listView 中添加标题视图,你应该在使用 setListAdapter() 之前严格执行此操作,否则会导致 IllegalStateException。

    【讨论】:

    • 您在 anticafe 提供相同答案 2 周后回答了您自己的问题,只是为了选择它作为正确答案。这根本不符合 SO 的精神。
    • 是的,如果您阅读 cmets 和更新,您会发现我自己找到了答案。我想回答这个问题,但由于我的声誉低下,我无法回答。毕竟,你记下了我的正确答案。这根本不是 SO 的精神。
    • 没有胜利者,你一开始没有发表任何评论。即使你这样做了,你也应该感谢有人为你做出了明确的回答。
    • 亲爱的 E. Gunyar。没有人给我一个明确的答案,很明显,你完全不知道你在说什么。无论如何,这个问题是 3 年前提出的......请不要这么重要。
    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多