我有一个 ListView Activity ,它需要项目列表的页脚,以便您可以单击它并将更多项目加载到列表中。该列表由我的 SimpleAdapter 支持,由字符串映射支持,在设置适配器之前,我这样做是为了添加页脚:
mInflater.inflate(R.layout.list_load_more_row, null);
TextView footer = (TextView) findViewById(R.id.loadMore);
getListView().addFooterView(footer);
setListAdapter(ListViewHelper.getAdapterForContentList(mContent, this));
但是我在调试器中遇到了这个异常
java.lang.NullPointerException异常 android.widget.ListView.clearRecycledState(ListView.java:489) android.widget.ListView.resetList(ListView.java:476) android.widget.ListView.setAdapter(ListView.java:417)
出了什么问题,我该如何将页脚添加到列表中?
[编辑] 该 Activity 正在使用 list_load_more_row.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#000000"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:gravity="center_horizontal|center_vertical"
android:text="@string/hello" />
最佳答案
好吧,我找到了解决我的问题的方法,如果我这样做它会按预期工作:
View view = mInflater.inflate(R.layout.list_load_more_row, null);
TextView footer = (TextView) view.findViewById(R.id.loadMore);
getListView().addFooterView(footer);
setListAdapter(ListViewHelper.getAdapterForContentList(mContent, this));
我猜,因为我在 inflater 中将 null 作为父参数,所以 View 还没有被添加到当前内容 View 中,所以 mainActivity 无法找到它,现在因为我明确地使用了父 View 由 inflater 返回以找到它正在工作的 TextView。
关于java - Android 将页脚添加到 ListView addFooterView()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3227182/