title: Android: how to get the views of a Fragment
body: <p>In a class i am adding a Fragment programatically.</p>
  
  <p>This Fragment contains a Button, which i want to have an onClick implementation.</p>
  
  <p>Here is the class i have:</p>
  
  <pre><code>public class ShareProduct extends SherlockFragmentActivity {
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.share_product);
  
          FragmentManager fragmentManager = getSupportFragmentManager();
          final FragmentTransaction fragmentTransaction = fragmentManager
                  .beginTransaction();
  
          ProductFragment1 fragment = new ProductFragment1();
          fragmentTransaction.add(R.id.share_product_parent, fragment);
          fragmentTransaction.commit();
  
          Button done_button = (Button) fragment.getView().findViewById(
                  R.id.done_product_1);
          done_button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View arg0) {
  
                  ProductFragment2 fragment = new ProductFragment2();
                  fragmentTransaction
                          .replace(R.id.share_product_parent, fragment);
                  fragmentTransaction.commit();
              }
          });
      }
  }
  </code></pre>
  
  <p>and my ProductFragment1 class is:</p>
  
  <pre><code>public class ProductFragment1 extends SherlockFragment {
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
      }
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater
                  .inflate(R.layout.share_product_1, container, false);
          return view;
      }
  }
  </code></pre>
  
  <p>and the layout my main class uses is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/share_product_parent"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:gravity="right|center_vertical"
      android:orientation="horizontal" &gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and the layout my ProductFragment1 uses is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/tv1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25dp" /&gt;
  
      &lt;EditText
          android:id="@+id/et1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/tv1"
          android:textSize="25dp" /&gt;
  
      &lt;Button
          android:id="@+id/done_product_1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/et1"
          android:text="Done" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>The problem is:</strong></p>
  
  <p>I am getting a NullPointerException at this line in my main class:</p>
  
  <pre><code>Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
  done_button.setOnClickListener ....
  </code></pre>
  
  <p>when i searched for what <code>getView()</code> on a fragment returns. I found out that it returns the view from <code>onCreateView();</code></p>
  
  <p>I checked it doesn't return null.</p>
  
  <p>Thank You</p>