findViewById() method returns null only sometimes (working fine most of the time)
<p>I am facing a very strange issue here. In my <code>fragment</code>, After I <code>inflate</code> the <code>layout</code>, <code>findViewById()</code> method returns null sometimes. But the issue is not reproducible at all. I am getting these crash reports from Crashlytics. And this crash happened with one of the QA members also but then he was also not able to reproduce this issue.</p>

<p>Here is the code for my fragment</p>

<pre><code>      @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View customView= inflater.inflate(R.layout.custom_view, container, false);
    ImageView back= (ImageView) customView.findViewById(R.id.back);

    //Throwing Null Pointer Exception for the below line.
    back.someFunc();
    return customView;
 }
</code></pre>

<p>XML File: </p>

<pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/topics_parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="?attr/default_background"&gt;

    &lt;ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="@dimen/back_to_top_margin_right"
        android:layout_marginBottom="@dimen/back_to_top_margin_bottom"
        android:src="@drawable/back"
        android:visibility="gone"
        android:id="@+id/back"/&gt;

    &lt;LinearLayout
        android:id="@+id/error_parent"
        android:layout_width="match_parent"
        android:background="?attr/default_background"
        android:orientation="vertical"
        android:layout_height="match_parent"&gt;
    &lt;/LinearLayout&gt;

    &lt;ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>Crash Logs</p>

<pre><code>    Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.news.activities.NewsHomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3928)
       at android.app.ActivityThread.access$900(ActivityThread.java:147)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5253)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
       at com.myapp.news.fragments.TopicsGroupFragment.onCreateView(TopicsGroupFragment.java:157)
       at android.app.Fragment.performCreateView(Fragment.java:2053)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
       at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1869)
       at android.app.Activity.performCreateCommon(Activity.java:5970)
       at android.app.Activity.performCreate(Activity.java:5977)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3928)
       at android.app.ActivityThread.access$900(ActivityThread.java:147)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5253)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
</code></pre>

<p>Having a really hard time figuring out why <code>findViewById()</code> is returning null for that view. </p>

<p>Note : I have many <code>ImageView</code> <code>resources</code> whose id is <code>back</code> but they are all in different XML files.</p>