我試圖使包含0-2標題和0-1頁腳(基於所需模式)的'動態'列表視圖。 如果我使用的順序NullPointerException將腳註項添加到Android ListView
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是不是在這一刻運行空
我想不出這裏有什麼問題,但我真的需要頁眉和頁腳是列表視圖的一部分(我可以用它做ScrollView和單獨的框架,但不會這樣做)
更新:要添加的東西:顯示在列表中的數據正在異步加載並存儲在單獨的靜態ArrayList中。我在加載開始和setAdapter(myAdapter)上使用setAdapter(null)完成修改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;
}
}
請包含'view_question_list_footer.xml'的XML和'myAdapter'的代碼.. –