Listview.setAdapter ...当我从另一个标签返回时崩溃

时间:2015-01-10 20:34:54

标签: java android android-listview android-arrayadapter

我有一个带有3个选项卡的Activity,首先是一个带有listview的片段。 listview有自定义适配器。当我开始和第二个标签回来时没有问题。但是当我转到第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();
    }
}

我的问题是当listview托盘第二次设置adadter时,我不知道重启listview如何开始像新的一样。请帮忙。

1 个答案:

答案 0 :(得分:1)

您在onCreateView内执行AsyncTask。此时,片段的视图层次结构尚未添加到活动中,因此如果您搜索片段的视图,getActivity().findViewById()将返回null

稍后尝试启动AsyncTask:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    new consultarRing().execute();
}