android Illegal state exception content view not yet create?

。_饼干妹妹 提交于 2019-12-20 04:23:42

问题


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

            }

        }
    });

}

回答1:


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.



来源:https://stackoverflow.com/questions/26351457/android-illegal-state-exception-content-view-not-yet-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!