title: Android textView.setText() crashing app
body: <p>I've looked all over for an answer but none of the general answers posted here or anywhere else are helping. I'm trying to set a TextView's text via code in the onCreate function of my Activity and it just won't work.</p>
  
  <p>Here is the Activity Code (Edit: with nested Fragment code):</p>
  
  <pre><code>import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.ActionBarActivity;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class NationalActivity extends ActionBarActivity {
  
  private String nationID;
  public static final String REVENUE_MESSAGE = "com.murrow.natlrevcalc.REVENUE";
  private int revenue;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_national);
  
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new NationalFragment()).commit();
      }
  
      Intent i = getIntent();
      nationID = i.getStringExtra(RunNationalsActivity.NATION);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.national, 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);
  }
  
  /**
   * National Fragment
   */
  public static class NationalFragment extends Fragment {
  
      public NationalFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_national,
                  container, false);
  
          TextView test = (TextView)getView().findViewById(R.id.townText);
          test.setText("This is not working");
  
          return rootView;
  
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Activity XML:</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.murrow.natlrevcalc.NationalActivity"
  tools:ignore="MergeRootFrame" &gt;
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>Fragment (where all of the information is) XML:</p>
  
  <pre><code>&lt;TableLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".NationalActivity$NationalFragment" &gt;
  
  &lt;ScrollView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" &gt;
  
      &lt;TableLayout
          android:id="@+id/nationalLayout"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" &gt;
  
          &lt;TextView
              android:id="@+id/townText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Towns"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;EditText
              android:id="@+id/yellowTown"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greenTown"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/brownTown"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;android.support.v7.widget.Space
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
  
          &lt;TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Cities"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;EditText
              android:id="@+id/yellowCity"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greenCity"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/brownCity"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greyCity"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;TextView
              android:id="@+id/TextView01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="GC"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;EditText
              android:id="@+id/yellowGC"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greenGC"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/brownGC"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greyGC"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;TextView
              android:id="@+id/TextView02"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Met"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;EditText
              android:id="@+id/yellowMet"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greenMet"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/brownMet"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;EditText
              android:id="@+id/greyMet"
              android:layout_width="52dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="number" /&gt;
  
          &lt;TextView
              android:id="@+id/textView4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
          &lt;Button
              android:id="@+id/run"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="run"
              android:text="Run" /&gt;
      &lt;/TableLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p></p>
  
  <p>And more specifically, the TextView in question:</p>
  
  <pre><code>&lt;TextView
              android:id="@+id/townText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Towns"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  </code></pre>
  
  <p>EDIT: LogCat Error:</p>
  
  <pre><code>04-23 09:50:18.881: E/AndroidRuntime(14665): FATAL EXCEPTION: main
  04-23 09:50:18.881: E/AndroidRuntime(14665): java.lang.RuntimeException: Unable to   start activity 
  
      ComponentInfo{com.murrow.natlrevcalc/com.murrow.natlrevcalc.NationalActivity}: java.lang.NullPointerException
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.os.Handler.dispatchMessage(Handler.java:99)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.os.Looper.loop(Looper.java:137)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.ActivityThread.main(ActivityThread.java:5419)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at java.lang.reflect.Method.invokeNative(Native Method)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at java.lang.reflect.Method.invoke(Method.java:525)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at dalvik.system.NativeStart.main(Native Method)
  04-23 09:50:18.881: E/AndroidRuntime(14665): Caused by: java.lang.NullPointerException
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at com.murrow.natlrevcalc.NationalActivity.onCreate(NationalActivity.java:95)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.Activity.performCreate(Activity.java:5372)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
  04-23 09:50:18.881: E/AndroidRuntime(14665):    ... 11 more
  </code></pre>
  
  <p>I've tried using a Handler, I've tried using/messing with threads. I've tried a bunch of suggestions that make no sense that I've found in other posts on this site. </p>
  
  <p>The Activity/Fragment relationship should be fine because the activity inflates the fragment (as far as I know).</p>
  
  <p>Please help!</p>
  
  <p>Thanks.</p>