title: Yet Another Case of findViewById() Returning null in a Fragment
body: <p>Yes, I know there are several topics on the matter of <em>findViewById() returns null</em> and no, none of them seems to solve my problem.</p>
  
  <p>Most of the solutions are about the timing on which the <code>findViewId()</code> is called, which I believe is not my case.</p>
  
  <p>So, I have a <code>MyFragment</code> class, for fragments that are displayed on a <code>ViewPager</code>. The class follows the suggested pattern for its creation (using a <code>newInstance()</code> method). </p>
  
  <p>In my <code>onCreateView</code> method, I find some <code>Views</code> in my newly inflated view (<code>rootView</code>), set some properties (typeface, text...) and listeners. One of the <code>Views</code> (in this example, <code>problematicLayout</code>) is supposed to be visibile/hidden depending on a condition. </p>
  
  <p><strong>Everything works fine until I rotate the device.</strong> As I do it, all the views in the <code>onCreateView()</code> are correctly found and their properties are set. <code>problematicLayout = (LinearLayout ) rootView.findViewById(R.id.problematicLayout)</code>, however, is returning <code>null</code>. If I pause and browse through the View hierarchy
   <strong>(rootView --> mChildren --> and so on)</strong> I can see that <code>problematicLayout</code> has its <code>ID = -1!</code></p>
  
  <p>I've also tried moving my <code>findViewById(R.id.problematicLayout)</code> to the <code>onViewCreated()</code>, using the method signature's view, only to achieve the same result.</p>
  
  <p>If I comment the line and never make the <code>problematicLayout</code> invisible/gone, I don't have the same issue.</p>
  
  <p>Why is this happening? Why is this <code>problematicLayout's</code> ID being set to -1, causing <code>findViewById()</code> to return null? What am I missing here?</p>
  
  <pre><code>public static Fragment newInstance(Context context,
      int position, Answer answer) {
  
      MyFragment frag = new MyFragment ();
  
      Bundle args = new Bundle();
      args.putParcelable(STUFF NAME, STUFF);
      frag.setArguments(args);
  
      return frag;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(
              R.layout.my_fragment, container, false);
  
      ScorllView someScrollView = (ScrollView) rootView.findViewById(R.id.someScrollView);
      // ... other views, setting their event listners, etc.
  
      LinearLayout problematicLayout = (LinearLayout ) rootView.findViewById(R.id.problematicLayout);
  
      setProblematicLayout(problematicLayout);
  
      // ... setting other listeners, etc.
      return rootView;
  
  }
  
  public void setProblematicLayout(LinearLayout layout)
  {
       if(!condition)
       {
           layout.setVisibiliy(View.GONE)
           // ... set listeners and properties for views inside the layout.
       }
       else
       {
           layout.setVisibiliy(View.INVISIBLE);
       }
  
  }
  </code></pre>