title: Accessing Fragment's views after inflating
body: <h2><strong>What I need to achieve</strong></h2>
  
  <p>A screen displaying a <code>ListView</code>, which can be replaced by an error screen in case of problems  (missing connection, server unavailable and the like).<br>
  I need to be able to switch (programmatically) back and forth between these two screens.</p>
  
  <h2><strong>Requirements</strong></h2>
  
  <p>The main screen must be a Fragment.<br>
  This is because my application is composed of several sections, each one accessible from the navigation drawer.</p>
  
  <h2><strong>What I have done so far</strong></h2>
  
  <p>The main fragment class is named <code>AllQueuesFragment</code>: its XML layout consists of a <code>FrameLayout</code>, which I use in combination with the <code>FragmentManager</code> to switch between <code>ErrorFragment</code> (containing the error message) and <code>QueuesViewFragment</code> (containing the <code>ListView</code>).</p>
  
  <pre><code>public class AllQueuesFragment extends Fragment
  {   
      public AllQueuesFragment()
      {
          super();
      }
  
      @Override
      public void onStart()
      {
          super.onStart();
  
          // Show the right fragment based on connectivity status
          checkConnection();
      }
  
      public void checkConnection()
      {
          final NetworkManager netManager = NetworkManager.getInstance(this.getActivity());
  
          if (netManager.isConnected())
              showQueues();
          else
              showNoConnection();     
      }
  
      public void showNoConnection()
      {
          ErrorFragment fragNoConnection = new ErrorFragment();
          displayFragment(fragNoConnection);
  
          fragNoConnection.setTitle(R.string.text_no_connection);
          fragNoConnection.setIcon(R.drawable.thatfeel);
          fragNoConnection.setLoaderVisibility(false);        
      }
  
      public void showQueues()
      {
          QueuesViewFragment fragQueuesView = new QueuesViewFragment();
          displayFragment(fragQueuesView);
      }   
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      {
          // Inflate the view
          View rootView = inflater.inflate(R.layout.fragment_allqueues, container, false);
  
          return rootView;
      }
  
      // Displays a new fragment
      public void displayFragment(Fragment fragment)
      {       
          if (fragment != null) 
          {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
          } 
      }                                       
  }
  </code></pre>
  
  <p>The error screen is the following:</p>
  
  <pre><code>public class ErrorFragment extends Fragment 
  {   
      private TextView textTitle;
  
      public ErrorFragment()
      {       
          super();
      }   
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      {
          // Inflate the view
          View rootView = inflater.inflate(R.layout.fragment_error, container, false);
  
          // Get the widgets
          textTitle = (TextView)rootView.findViewById( R.id.fragment_error_text );
  
          return rootView;
      }
  
  
      // Set methods
      public void setTitle(int id) { textTitle.setText(id); }
  }
  </code></pre>
  
  <h2><strong>The problem</strong></h2>
  
  <p>The <code>setTitle()</code> method gets called before the layout is ready, and as a result, a <code>NullPointerException</code> is thrown.  </p>
  
  <pre><code>class AllQueuesFragment
  {
      ....
  
      public void displayFragment(Fragment fragment)
      {       
          if (fragment != null) 
          {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
          } 
      }   
  
      public void showNoConnection()
      {
          ErrorFragment fragNoConnection = new ErrorFragment();
          displayFragment(fragNoConnection);
  
          // PROBLEM HERE: Before calling setTitle(), I must be sure that ErrorFragment's 
          // layout is inflated!
          fragNoConnection.setTitle(R.string.text_no_connection);
      }
  
      ....
  }
  
  class ErrorFragment
  {
      ....
      public void setTitle(String value) { textTitle.setText(value); }
      ....
  }
  </code></pre>
  
  <p>I can't call <code>setTitle()</code> directly from <code>ErrorFragment::onCreateView()</code>, because I don't know beforehand which message I need to show.</p>
  
  <p>How can I ensure that <code>fragNoConnection</code> has completed its layouting?<br>
  Is there a better way to achieve my goal?</p>
  
  <h2>Unsatisfying workaround</h2>
  
  <p>The only workaround I can think of is to use a buffer to defer the actual call:</p>
  
  <pre><code>class ErrorFragment
  {
      // This string will hold the title until the layout is inflated
      private String titleBuffer;
      private TextView textTitle = null;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      {
          // Inflate the view
          View rootView = inflater.inflate(R.layout.fragment_error, container, false);
  
          // Get the widgets
          textTitle = (TextView)rootView.findViewById( R.id.fragment_error_text );
  
          // Do the actual set
          setTitle(titleBuffer);
  
          return rootView;
      }
  
      ....
  
      public void setTitle(String value) 
      { 
              titleBuffer = value;
  
              // If the layout is not inflated, defer the actual set
              if (textTitle != null)
                      textTitle.setText(titleBuffer); 
      }
      ....
  }
  </code></pre>
  
  <p>but I don't like this solution very much (the code above is simplified; <code>ErrorFragment</code> has more properties).</p>
  
  <p>Advices?<br>
  Thanks in advance</p>