title: Fragment getView().findViewById(R.id.box1) Returning null
body: <p>So here's the problem. I'm coding an app with two EditText views inside a fragment and a button. When the button is clicked, it changes the text of the EditText. In the onViewCreated() method I use this code to get the EditText instance and store it to a variable.</p>
  
  <pre><code>EditText box1 = (EditText)(getView.findViewById(R.id.box1));
  </code></pre>
  
  <p>This works fine. However, when I try to access variable box1 when the button is clicked, the EditText has become null and throws a NullPointerException. Does anyone know why this would happen?</p>
  
  <p>Here is the fragment code:</p>
  
  <pre><code>public class MyFragment extends Fragment {
  
  EditText box1, box2;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_text_coder_free,
                  container, false);
  
          //Works here.
          box1 = (EditText)(rootView.findViewById(R.id.box1));
          box2 = (EditText)(rootView.findViewById(R.id.box2));
          //Same thing. Sets the text correctly
          box1.setText("hey look at me!");
          return rootView;
      }
  
      //Called when the button is clicked.
      public void setBox1Text(String text) {
          //It's null here so it skips to the else
          if(box1 != null) {
              box1.setText(text);
          }
          else {
              //This doesn't work. Throws the exception here.
              box1 = (EditText)(getView().findViewById(R.id.box1));
          }
      }
  
      public void setBox2Text(String text) {
          if(box2 != null) {
              box2.setText(text);
          }
          else {
              //This doesn't work. Throws the exception here.
              box2 = (EditText)(getView().findViewById(R.id.box2));
          }
      }
  
      public String getBox1Text() {
          if(box1 == null) {
              return "Nice try.";
          }
          return box1.getText().toString();
      }
  
      public String getBox2Text() {
          if(box2 == null) {
              return "Nice try.";
          }
          return box2.getText().toString();
      }
  }
  </code></pre>
  
  <p>Here is the activity which houses the fragment:</p>
  
  <pre><code>public class MyActivity extends Activity {
  
      MyFragment myFragment
      EditText box1, box2;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_text_coder);
  
          if (savedInstanceState == null) {
              myFragment = new MyFragment();
              getFragmentManager().beginTransaction()
                      .add(R.id.container, myFragment.commit();
          }
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.my_app, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
  
      public void box1Click(View v) { 
          myFragment.setBox2Text("Some text");
      }
  
      public void box2Click(View v) {
          myFragment.setBox2Text("Some text");
      }
  
  }
  </code></pre>
  
  <p>And here is the XML from the fragment:</p>
  
  <pre><code>&lt;RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.mycode.MyFragment" &gt;
  
  &lt;EditText
      android:id="@+id/box1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:inputType="textMultiLine" /&gt;
  &lt;EditText
      android:id="@+id/box2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/box1"
      android:inputType="textMultiLine|none"
      android:focusable="false" /&gt;
  &lt;Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/box2"
      android:layout_alignParentRight="true"
      android:onClick="box2Click" /&gt;
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/encrypt_button_text"
      android:layout_below="@id/box2"
      android:layout_toLeftOf="@id/button2"
      android:onClick="box1Click" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>