尚未在listfragment中创建的Illegalstateexception内容视图

时间:2014-04-07 17:59:41

标签: android android-fragments android-listfragment

问题是,当我搜索listfragment时,它可以工作。当我关闭它并再次打开并进行另一次搜索时,会出现异常。在开发人员选项中,我选择不维护活动。

我想当应用程序唤醒时,布局的引用会丢失,但不知道为什么。

我该怎么办?

这是我的代码

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    listAdapter = new ListAdapter(getActivity(), adArrayList);
    setListAdapter(listAdapter);
    getListView().setOnScrollListener(this);

}

@Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view,savedInstanceState);

    ViewGroup viewGroup = (ViewGroup) view;

    mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());

    ActionBarPullToRefresh.from(getActivity())

            .insertLayoutInto(viewGroup)

            .theseChildrenArePullable(android.R.id.list)

            .listener(this)

            .setup(mPullToRefreshLayout);
}

@Override
    public void onResume() {
        pagination = 0;
        getAds("");
        super.onResume() ;

    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = super.onCreateView(inflater, container, savedInstanceState);
    listView = (ListView) view.findViewById(android.R.id.list);
    ViewGroup parent = (ViewGroup) listView.getParent();

    // Remove ListView and add CustomView in its place
    int listViewIndex = parent.indexOfChild(listView);
    parent.removeViewAt(listViewIndex);
    LinearLayout linearLayout = (LinearLayout) inflater.inflate(
            R.layout.list_frag, container, false);
    parent.addView(linearLayout , listViewIndex,
            listView.getLayoutParams());

    listView.setDrawSelectorOnTop(false);
    listView.setDivider(null);
    listView.setDividerHeight(0);

    return view;
}

这是我用来从服务器获取数据的方法

 //the next method use retrofit
        public void getAds(String productSearch){
                isLoadingMoreAds = true;

                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    mPullToRefreshLayout.setRefreshing(true);
                } else{
                    setListShown(false);
                }
                Services services= Utils.getRESTAdapter();

                services.getSearch(new Callback<AdsList>() {
                    @Override
                    public void success(AdsList adsList, Response response) {
                        try{
                            setListShown(true);
                            mPullToRefreshLayout.setRefreshComplete();
                            updateView(adsList.getAd());
                            isLoadingMoreAds = false;
                        }catch(IllegalStateException e){
                            Log.e("Exception", "ListFragment ya no existe");
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {

                        isLoadingMoreAds = false;

                        mPullToRefreshLayout.setRefreshComplete();

                        updateView(new ArrayList<Ad>());

                        setListShown(true);

                        Log.e("Error", "Failure at get ads");

                    }
                });

            }

0 个答案:

没有答案