Search code examples
androidparse-platformandroid-listfragment

android Illegal state exception content view not yet create?


Hi when I start my drawer activity fragment, the illegal state exception content view not yet create error pop up. here is my code and error. I am using an custom list adapter.

error:

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)

my 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;
}

mysetlist adapter call in onResume

@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);
                }

            }

        }
    });

}

Solution

  • Move the following to onActivityCreated() method or in 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);
                    }
    
                }
    
            }
        });
        }
    

    A good practice is to put everything that uses UI widgets inside onActivityCreated() method.