Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment is given a null container in onCreateView()

I am trying to use Android fragments in a very simple way, similar to the tutorial on the Android developer website.

I have an Activity (MediaInfoActivity) with the following code:

public class MediaInfoActivity extends FragmentActivity {
    private final String TAG = "MediaInfoActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate()");
        setContentView(R.layout.media_info_activity_layout);
    }

}

Here is the code for the media_info_activity_layout.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment class="com.hawkforce.test.MediaInfoFragment"
            android:id="@+id/mediaInfoFragment"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

    <FrameLayout android:id="@+id/mediaPlayerBarPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

    <fragment class="com.hawkforce.test.MediaPlayerBarFragment"
                android:id="@+id/mediaPlayerBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

    </FrameLayout>

And finally here is the code for MediaInfoFragment:

public class MediaInfoFragment extends Fragment {
    private final static String TAG = "MediaInfoFragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate()");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView()");
        if (container == null) {
            Log.i(TAG, "onCreateView(): container = null");
        }

        return inflater.inflate(R.layout.media_info_fragment_layout, container, false);
    }

}

Here is my problem : the container passed in the onCreateView() method of the MediaInfoFragment is null. As I understood, this should only be the case for non-UI Fragments. However, my Fragment has a UI, which is displayed OK on the screen when I launch MediaInfoActivity. It causes problems because no style declared in the xml layout file of the fragment is applied.

Here is my Log:

I/MediaInfoActivity: onCreate()
I/MediaInfoFragment: onCreate()
I/MediaInfoFragment: onCreateView()
I/MediaInfoFragment: onCreateView(): container = null

Am I missing anything obvious here ?

like image 765
HawkForce Avatar asked Jun 02 '13 14:06

HawkForce


People also ask

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

What is onCreateView fragment?

onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

When onviewcreated is called?

oncreate view instantiates the view, onviewcreated is called after oncreateview and before saved states are restored... it's more a timing issue in the lifecycle of the fragment. – me_ Oct 23, 2018 at 5:57.

What is an Android Fragment?

According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.


1 Answers

You just have to create a inflater like bellow in your fragment.

View rootView;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (rootView == null) {
        rootView = inflater.inflate(R.layout.activity_my_cart, null);
    } else {
        ((ViewGroup) container.getParent()).removeView(rootView);
    }
    return rootView;
}

I hope it will work as per your question.

like image 137
Namdev Londhe Avatar answered Oct 13 '22 00:10

Namdev Londhe