Android的 - 这是为什么告诉我"内容视图尚未创建"?这是、告诉我、视图、内容

2023-09-04 03:33:00 作者:满目星河

这是从数据库中一个片段的填充一个ListView:

This is a populating a listview on a fragment from a database:

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

            LinearLayout Layout5 = (LinearLayout) inflater.inflate(R.layout.tab_frag5_layout, container, false);

            Cursor allBands;
            MyDatabase db;

            Context ctx = (Context)TabFragment5.this.getActivity();


            db = new MyDatabase(ctx);
            allBands = db.getBands();


            ListAdapter adapter = new SimpleCursorAdapter (ctx, 
                    R.layout.listelement, 
                    allBands, 
                    new String[] {"BandName"},  
                    new int[] {R.id.text15});

            getListView().setAdapter(adapter);  

            return Layout5;

         }

这是为什么给我开的logcat在尚未创建内容视图?节目部队密切片段打开时...

Why is this giving me the "Content view not yet created" On logcat? the program forces close when the fragment opens...

推荐答案

我通过移动适配器和getListview到onActivityCreated(...)解决了这个问题。

I solved it by moving the adapter and the getListview to onActivityCreated(...).

onCreateView只是膨胀并返回布局

onCreateView just inflates and returns the layout.