title: Accessing Views inside fragment
body: <p>I'm having troubles accessing the Views that within my Fragment. In the example below, I can't access the 2nd button that is within the Fragment (e.g., <em>findViewById</em> appears to return NULL and the app crashes when I try b2.setText("test") ), but when I directly add it in the activity_main.xml, it does work.</p>
  
  <p>Here is the code:</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity 
  {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      final Button fragmentButton = (Button)findViewById(R.id.iMainButton);
  
      FragmentManager fm = getFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();
      ft.add(R.id.iMainInnerLLContainer, new TestFragment());         
      ft.commit();
  
      final Button b2 = (Button)findViewById(R.id.iTestFragmentInnerButton);
      if(b2 == null) { Log.d("MainActivity.java:", "b2 is null"); } 
      else { Log.d("MainActivity.java:", "b2 is NOT null"); } 
  
      fragmentButton.setOnClickListener(new View.OnClickListener() 
      {
          @Override
          public void onClick(View v) 
          {
              // do stuff here
          }
      });
  }
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout 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:id="@+id/iMainRootLinearLayoutContainer"
  android:orientation="vertical" 
  &gt;
  
  &lt;Button
      android:id="@+id/iMainButton"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:text="Add fragment..." 
  /&gt;    
  
  &lt;LinearLayout
      android:id="@+id/iMainInnerLLContainer"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:orientation="vertical"
  &gt;
      &lt;!-- Fragment goes here, can reference button if it is added here manually --&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>TestFragment.java</strong></p>
  
  <pre><code>public class TestFragment extends Fragment 
  {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
      return inflater.inflate(R.layout.ltestfragment, container, false);
  }
  }
  </code></pre>
  
  <p><strong>ltestfragment.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" 
  android:id="@+id/iTestFragmentOuterLinearLayout"
  &gt;
  
  &lt;Button
      android:id="@+id/iTestFragmentInnerButton"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:text="This is my test fragment button." 
  /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I think I'm missing something very basic here and I would appreciate some insight on what that might be. </p>
  
  <p>Thank you in advance!</p>