Android的非法状态异常的内容来看尚未创造?异常、状态、内容、Android

2023-09-07 18:16:04 作者:此生不换的執著

当我开始我的抽屉里的活动片段,在非法状态异常内容视图尚未创建错误弹出。这里是我的code和错误。我使用的是自定义列表适配器。

错误:

 九月10日至14号:40:25.926:E / AndroidRuntime(6736):java.lang.IllegalStateException:尚未创建内容的浏览
9月10日至十四号:40:25.926:E / AndroidRuntime(6736):在android.app.ListFragment.ensureList(ListFragment.java:386)
9月10日至十四号:40:25.926:E / AndroidRuntime(6736):在android.app.ListFragment.getListView(ListFragment.java:280)
9月10日至十四号:40:25.926:E / AndroidRuntime(6736):在com.example.fragments.HomeFragment $ 1.done(HomeFragment.java:74)
 

我oncreateview

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

    查看rootView = inflater.inflate(
            R.layout.fragment_home,集装箱,假);
    列表视图=(ListView控件)rootView.findViewById(android.R.id.list);
    返回rootView;
}
 

在onResume mysetlist适配器通话

  @覆盖
公共无效onResume(){
    super.onResume();

    ParseQuery<的parseObject>查询=新ParseQuery<的parseObject>(Shopinfo);
    query.findInBackground(新FindCallback<的parseObject>(){

        @覆盖
        公共无效完成(名单<的parseObject> Shopinfo,ParseException的E){
            // TODO自动生成方法存根
            如果(E == NULL){
                MSHOP = Shopinfo;
                的String []点=新的String [mShop.size()];
                INT I = 0;
                对于(的parseObject Shopinfos:MSHOP){
                    点[I] = Shopinfos.getString(ParseConstants.KEY_SHOP_NAME);
                    我++;
                }

                如果(getListView()。getAdapter()== NULL){
                    适配器=新ShopListAdapter(list.getContext(),MSHOP);
                    setListAdapter(适配器);
                }
                其他 {
                    ((ShopListAdapter)getListView()getAdapter()。)填充(MSHOP)。
                }

            }

        }
    });

}
 
Android学习 搭建android开发环境 详细

解决方案

将以下为 onActivityCreated()方法,或者在 onViewCreated()

  @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);
    ParseQuery<的parseObject>查询=新ParseQuery<的parseObject>(Shopinfo);
    query.findInBackground(新FindCallback<的parseObject>(){

        @覆盖
        公共无效完成(名单<的parseObject> Shopinfo,ParseException的E){
            // TODO自动生成方法存根
            如果(E == NULL){
                MSHOP = Shopinfo;
                的String []点=新的String [mShop.size()];
                INT I = 0;
                对于(的parseObject Shopinfos:MSHOP){
                    点[I] = Shopinfos.getString(ParseConstants.KEY_SHOP_NAME);
                    我++;
                }

                如果(getListView()。getAdapter()== NULL){
                    适配器=新ShopListAdapter(list.getContext(),MSHOP);
                    setListAdapter(适配器);
                }
                其他 {
                    ((ShopListAdapter)getListView()getAdapter()。)填充(MSHOP)。
                }

            }

        }
    });
    }
 

一个好的做法是把使用UI部件放在 onActivityCreated一切()方法。

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

            }

        }
    });

}

解决方案

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.