android非法状态异常内容视图尚未创建?

时间:2014-10-14 01:46:58

标签: android parse-platform android-listfragment

当我启动抽屉活动片段时,非法状态异常内容视图尚未创建错误弹出窗口。这是我的代码和错误。我正在使用自定义列表适配器。

错误:

10-14 09:40:25.926: E/AndroidRuntime(6736): java.lang.IllegalStateException: Content view not yet created
10-14 09:40:25.926: E/AndroidRuntime(6736):     at android.app.ListFragment.ensureList(ListFragment.java:386)
10-14 09:40:25.926: E/AndroidRuntime(6736):     at android.app.ListFragment.getListView(ListFragment.java:280)
10-14 09:40:25.926: E/AndroidRuntime(6736):     at com.example.fragments.HomeFragment$1.done(HomeFragment.java:74)

我的oncreateview

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

    View rootView = inflater.inflate(
            R.layout.fragment_home, container, false);
    listview=(ListView) rootView.findViewById(android.R.id.list);
    return rootView;
}

onResume中的mysetlist适配器调用

@Override
public void onResume() {
    super.onResume();

    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Shopinfo");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> Shopinfo, ParseException e) {
            // TODO Auto-generated method stub
            if(e==null){
                mShop=Shopinfo;
                String[] spots = new String[mShop.size()];
                int i = 0;
                for(ParseObject Shopinfos : mShop) {
                    spots[i] =  Shopinfos.getString(ParseConstants.KEY_SHOP_NAME);
                    i++;
                }

                if (getListView().getAdapter() == null) {
                    adapter=new ShopListAdapter(list.getContext(), mShop);
                    setListAdapter(adapter);
                }
                else {
                    ((ShopListAdapter)getListView().getAdapter()).refill(mShop);
                }

            }

        }
    });

}

1 个答案:

答案 0 :(得分:0)

将以下内容移至onActivityCreated()方法或onViewCreated()

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Shopinfo");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> Shopinfo, ParseException e) {
            // TODO Auto-generated method stub
            if(e==null){
                mShop=Shopinfo;
                String[] spots = new String[mShop.size()];
                int i = 0;
                for(ParseObject Shopinfos : mShop) {
                    spots[i] =  Shopinfos.getString(ParseConstants.KEY_SHOP_NAME);
                    i++;
                }

                if (getListView().getAdapter() == null) {
                    adapter=new ShopListAdapter(list.getContext(), mShop);
                    setListAdapter(adapter);
                }
                else {
                    ((ShopListAdapter)getListView().getAdapter()).refill(mShop);
                }

            }

        }
    });
    }

一个好的做法是将使用UI小部件的所有内容放在onActivityCreated()方法中。