Why findViewById() return null for a view in a drawer header?
<p>I have a <code>DrawerLayout</code> which wraps a <code>NavigationView</code> with a header layout. The XML is below:</p>

<pre><code>&lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                        xmlns:app="http://schemas.android.com/apk/res-auto"
                                        android:id="@+id/drawer_layout"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"&gt;

    &lt;android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer"/&gt;

&lt;/android.support.v4.widget.DrawerLayout&gt;
</code></pre>

<p>The <code>headerLayout</code> is a <code>LinearLayout</code> which wraps a <code>ImageView</code>, below is <code>drawer_header.xml</code>:</p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@color/blue_primary"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;

    &lt;ImageView
        android:layout_width="120dp"
        android:id="@+id/ivDrawerUserFace"
        android:layout_height="120dp" /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>I don't know why I can't find the <code>ImageView</code> by <code>findViewById(R.id.ivDrawerUserFace)</code>, which returns <code>null</code> instead. However, I am able to find it by the following code:</p>

<pre><code>ViewGroup headerView = (ViewGroup) mNavigationView.getHeaderView(0);
ImageView faceView = (ImageView) headerView.getChildAt(0); // faceView is not null
String faceUrl = QingUApp.getInstance().getFaceUrl();
if (faceUrl != null) {
     Glide.with(this).load(faceUrl).into(faceView);
}
View viewById = findViewById(R.id.ivDrawerUserFace); // viewById is null
</code></pre>

<p>I have called <code>setContentView()</code> before the above code. And <code>Glide</code> loads the image into the <code>ImageView</code> successfully. Why can't I find it by <code>findViewById()</code>?</p>

<p>I guess it's because the image view isn't within the XML file passed to <code>setContentView()</code>. However, I could get the view by <code>findViewById()</code> in the same <code>Activity</code> with code like this:</p>

<pre><code>private LoaderManager.LoaderCallbacks&lt;Bitmap&gt; mUserFaceLoaderCallbacks = new LoaderManager.LoaderCallbacks&lt;Bitmap&gt;() {
    @Override
    public Loader&lt;Bitmap&gt; onCreateLoader(int i, Bundle bundle) {
        return new UserFaceLoader(NewQingUActivity.this, mFaceUrl);
    }

    @Override
    public void onLoadFinished(Loader&lt;Bitmap&gt; loader, Bitmap bitmap) {
        if (bitmap != null) {
            ImageView ivUserFace = (ImageView) findViewById(R.id.ivDrawerUserFace);
            ivUserFace.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onLoaderReset(Loader&lt;Bitmap&gt; loader) {

    }
};
</code></pre>