Search code examples
androidlayoutfragmentfindviewbyid

Using Fragment layour ..... findbyViewId always NULL


I am pretty newbie with android development and I am trying to make work an easy example.

When the layout contains fragments, this fragments are inflated and the method findByViewId returns a NULL pointer. I would like to konw how to avoid that.

activity_main.xml 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.todoit.MainActivity"
    tools:ignore="MergeRootFrame" />

so far the only solution I found was to dont use fragment layout :/

fragment_main.xml 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.todoit.MainActivity$PlaceholderFragment">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myEditText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myListView"
        android:layout_below="@+id/myEditText"
        android:layout_alignRight="@+id/myEditText"
        android:footerDividersEnabled="false" /> </RelativeLayout>

this code returned a NULL pointer

setContentView(R.layout.activity_main); ListView myListView= (ListView)findViewById(R.id.myListView);

the only way I found was to use this one instead.

setContentView(R.layout.fragment_main); ListView myListView= (ListView)findViewById(R.id.myListView);

all this code in the onCreate method.

could someone tell me how to solve this issue? is there a way to inflate the whole layout including fragments?

thanks a lot. Jose


Solution

  • You should inflate the fragment's view in the fragment code only, not in the activity code. You use onCreateView method to do this.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_main, container, false);
        ListView myListView= (ListView) v.findViewById(R.id.myListView);
        return v;
    }
    

    If you want to find your ListView after onCreateView has already been called, you can use getView() method:

    ListView myListView= (ListView) getView().findViewById(R.id.myListView);