Android - why is this telling me “Content view not yet created”?

前端 未结 3 951
别那么骄傲
别那么骄傲 2020-12-14 08:20

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

public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle saved         


        
相关标签:
3条回答
  • 2020-12-14 08:56

    A Fragment should usually put inside an Activity while the onCreateView() will contribute the layout of Fragment to its container Activity.

    Quoted from http://developer.android.com/guide/topics/fundamentals/fragments.html

    A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity.

    So, the problem may probably be caused by the missing of setContentView() in your container Activity instead of your Fragment.

    0 讨论(0)
  • 2020-12-14 08:59

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

    onCreateView just inflates and returns the layout.

    0 讨论(0)
  • 2020-12-14 09:00

    I had the same issue but my fault was to call a (invisible) fragment from a background task via an interface. so the invisible fragment tried to use its view which was not available... i fixed it with the same solution: the interface function checks if fragment isVisible(). Thank you for showing me the right direction...

     public void updateListInterface(){
        if(this.isVisible()) {
            this.initListAdapter();
            getLoaderManager().restartLoader(0, null, this);
        } else {
            Log.v(TAG, "view is not visible");
        }
    }
    
    0 讨论(0)
提交回复
热议问题