Listview.setAdapter...当我从另一个选项卡返回时崩溃
Listview.setAdapter... crash when I come back from another tab
我有一个带有 3 个选项卡的 Activity,其中第一个是带有列表视图的片段。列表视图具有自定义适配器。当我开始时以及当我转到第二个选项卡并返回时都没有问题。但是当我转到第 3 个选项卡并返回到第一个选项卡时,片段执行 onCreateView 并因 nullpointerexception 而崩溃,这是我的代码:
片段:
List<Post> rings;
View headerView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_within_ring, container, false);
headerView = getActivity().getLayoutInflater().inflate(R.layout.header_list_view, null);
new consultarRing().execute(); //This get the Array from web service to rings
return rootView;
}
private void fullyAdapter() {
if (rings != null) {
adapter = new PostsAdapter(getActivity(), R.layout.item_row_show_ring, rings, false, null);
lyt = (PullAndLoadListView) getActivity().findViewById(R.id.listView_withinring);
lyt.addHeaderView(headerView); //crash here
lyt.setAdapter(adapter); //crash here
//crash all down
lyt.setOnRefreshListener(new OnRefreshListener() {
//OnRefreshListener
@Override
public void onRefresh() {
// TODO Auto-generated method stub
new PullToRefreshDataTask().execute();
}
});
lyt.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
new LoadMoreDataTask().execute();
}
});
lyt.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
switch (firstVisibleItem) {
case 1:
Main.isScrolling = false;
break;
case 2:
Main.isScrolling = true;
break;
default:
break;
}
}
});
}
}
private class consultarRing extends AsyncTask<String, String, CollectionResponsePost> {
@Override
protected void onPreExecute() {
//do staff
}
@Override
protected CollectionResponsePost doInBackground(String... args) {
//do staff, get rings
}
@Override
protected void onPostExecute(CollectionResponsePost responsePost) {
//call fullyAdapter() method
fullyAdapter();
}
}
我的问题是当列表视图托盘第二次设置适配器时,我不知道如何重置列表视图以像新的一样启动。请帮忙。
您在 onCreateView
内执行 AsyncTask。在那一刻,片段的视图层次结构尚未添加到 activity 中,因此如果您搜索片段的视图,getActivity().findViewById()
将 return null
。
稍后尝试启动 AsyncTask:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new consultarRing().execute();
}
我有一个带有 3 个选项卡的 Activity,其中第一个是带有列表视图的片段。列表视图具有自定义适配器。当我开始时以及当我转到第二个选项卡并返回时都没有问题。但是当我转到第 3 个选项卡并返回到第一个选项卡时,片段执行 onCreateView 并因 nullpointerexception 而崩溃,这是我的代码:
片段:
List<Post> rings;
View headerView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_within_ring, container, false);
headerView = getActivity().getLayoutInflater().inflate(R.layout.header_list_view, null);
new consultarRing().execute(); //This get the Array from web service to rings
return rootView;
}
private void fullyAdapter() {
if (rings != null) {
adapter = new PostsAdapter(getActivity(), R.layout.item_row_show_ring, rings, false, null);
lyt = (PullAndLoadListView) getActivity().findViewById(R.id.listView_withinring);
lyt.addHeaderView(headerView); //crash here
lyt.setAdapter(adapter); //crash here
//crash all down
lyt.setOnRefreshListener(new OnRefreshListener() {
//OnRefreshListener
@Override
public void onRefresh() {
// TODO Auto-generated method stub
new PullToRefreshDataTask().execute();
}
});
lyt.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
new LoadMoreDataTask().execute();
}
});
lyt.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
switch (firstVisibleItem) {
case 1:
Main.isScrolling = false;
break;
case 2:
Main.isScrolling = true;
break;
default:
break;
}
}
});
}
}
private class consultarRing extends AsyncTask<String, String, CollectionResponsePost> {
@Override
protected void onPreExecute() {
//do staff
}
@Override
protected CollectionResponsePost doInBackground(String... args) {
//do staff, get rings
}
@Override
protected void onPostExecute(CollectionResponsePost responsePost) {
//call fullyAdapter() method
fullyAdapter();
}
}
我的问题是当列表视图托盘第二次设置适配器时,我不知道如何重置列表视图以像新的一样启动。请帮忙。
您在 onCreateView
内执行 AsyncTask。在那一刻,片段的视图层次结构尚未添加到 activity 中,因此如果您搜索片段的视图,getActivity().findViewById()
将 return null
。
稍后尝试启动 AsyncTask:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new consultarRing().execute();
}