将页脚项添加到Android ListView的NullPointerException

时间:2011-06-22 12:33:06

标签: java android exception listview footer

我正在尝试制作包含0-2页眉和0-1页脚(基于所需模式)的'动态'列表视图。 如果我使用订单

getListView().addFooterView(footerView, null, true);
getListView().setAdapter(myAdapter);

我收到错误:

java.lang.NullPointerException
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)
at android.widget.ListView.addFooterView(ListView.java:364)
at com.my.fragments.QuestionListFragment.updateList(QuestionListFragment.java:63)

如果我将线路切换到

getListView().setAdapter(myAdapter);
getListView().addFooterView(footerView, null, true);

没有错误,但是在模式更改时没有显示页脚视图(它出现在下一个模式更改中,因此我假设代码正在运行)。

目前

footerView在运行时

中不为null

我无法弄清楚这里有什么问题,但我真的需要页眉和页脚成为listview的一部分(我可以使用ScrollView和单独的框架,但这不会做)

更新:要添加的内容:列表中显示的数据是异步加载的,并存储在单独的静态ArrayList中。我在加载开始时使用setAdapter(null),在完成时使用setAdapter(myAdapter)来修改ListView。也许问题就在这里?

UPDATE2 view_question_list_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_marginBottom="2dip">
    <TextView android:textAppearance="?android:attr/textAppearanceSmall" android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:textStyle="bold" android:text="Load More Questions" android:gravity="center"
        android:layout_gravity="center" android:lines="3" android:textColor="@android:color/white"></TextView>
</FrameLayout>

QuestionAdapter.java

public class QuestionAdapter extends BaseAdapter {

    public class BrowseSettings {
        public SortingMode  sortingMode     = SortingMode.SORT_BY_DATE;
        public boolean          isAnswered      = true;
        public int              categoryFilter  = -1;
        public String           textFilter      = null;
    }

    private LayoutInflater  inflater            = null;
    private BrowseSettings  settings            = new BrowseSettings();
    private LoaderListener  loaderListener  = null;

    public QuestionAdapter(Context context, LoaderListener listener) {
        inflater = LayoutInflater.from(context);
        loaderListener = listener;
    }

    private void loadData() {
        QAData.questions.clear();
        appendData();
    }

    public void appendData() {
        new QuestionLoaderThread(settings.isAnswered, settings.sortingMode, settings.categoryFilter, settings.textFilter,
                QAData.questions.getCount(), BaseRequest.DEFAULT_COUNT_VALUE, loaderListener).start();
    }

    public void applyCategoryFilter(int filter) {
        settings.categoryFilter = filter;
        loadData();
    }

    public void applyTextFilter(String filter) {
        settings.textFilter = filter;
        loadData();
    }

    @Override
    public int getCount() {
        return QAData.questions.getCount();
    }

    @Override
    public Object getItem(int position) {
        return QAData.questions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return QAData.questions.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        QuestionViewHolder viewHolder = null;
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.view_question, null);
            viewHolder = new QuestionViewHolder();
            viewHolder.content = (TextView) convertView.findViewById(R.id.QuestionContent);
            viewHolder.likes = (TextView) convertView.findViewById(R.id.QuestionLikeCounter);
            viewHolder.question = QAData.questions.get(position);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (QuestionViewHolder) convertView.getTag();
        }

        viewHolder.content.setText(Html.fromHtml(viewHolder.question.getFormattedQuestion()));
        viewHolder.likes.setText(String.valueOf(viewHolder.question.getLikesCount()));

        return convertView;
    }

    private class QuestionViewHolder {
        private TextView content;
        private TextView likes;
        private QAQuestion question;
    }
}

1 个答案:

答案 0 :(得分:1)

请尝试使用getListView().addFooterView(footerView);