title: Display a textview and button at the same time in Android app
body: <p>I am a bit new to Android apps and are making a simple app to send data between two classes using intents. (Android developer tutorial first app <a href="https://developer.android.com/training/basics/firstapp/index.html" rel="nofollow">https://developer.android.com/training/basics/firstapp/index.html</a>). I am currently experimenting with adding a button to go to another activity while displaying a textview on the same layout. The problem is, I cannot seem to get the button to show up the same time as the textview. I tried showing/hiding the button in my next class's OnCreate method, and now have tried to dynamically create the layout that I want. The problem I am getting is with my LinearLayout.addView(textView) option, I am getting a null pointer. Can anyone offer any advice as to what is wrong with my code or if I should scrap this idea and try a new one all together. I'm still very new and open to all suggestions. Thanks!</p>
  
  <p><b>Activity</b></p>
  
  <pre><code>package com.example.app;
  
  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.Button;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  public class DisplayMessageActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Intent intent = getIntent();
  
          //textView.setVisibility(backbutton.VISIBLE);
          // Set the text view as the activity layout
          setContentView(R.layout.fragment_main);
          LinearLayout rl = (LinearLayout)findViewById(R.id.relative_layout);
          String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
          TextView textView = new TextView(this);
          textView.setTextSize(20);
          message = "The current address is " + message;
          textView.setText(message);
          Button backbutton;
          backbutton = (Button) this.findViewById(R.id.back);
          rl.addView(textView); //Null pointer exception HERE
          rl.addView(backbutton);
          setContentView(rl);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.display_message, 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);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_display_message,
                      container, false);
              return rootView;
          }
      }
  
  }
  </code></pre>
  
  <p><b>XML File</b></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/relative_layout"
      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=".DisplayMessageActivity" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
       /&gt;
      &lt;Button
          android:id="@+id/back"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:onClick="Back"
          android:text="@string/back" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><b>Manifest</b></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.app"
      android:versionCode="1"
      android:versionName="1.0" &gt;
      &lt;uses-permission android:name="android.permission.CAMERA"/&gt;
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.app.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name="com.dm.zbar.android.scanner.ZBarScannerActivity"
          android:screenOrientation="landscape"
          android:label="@string/app_name" /&gt;
          &lt;activity
          android:name="com.example.app.DisplayMessageActivity"
          android:label="@string/title_activity_display_address"
          android:parentActivityName="com.example.app.MainActivity" &gt;
          &lt;meta-data
              android:name="android.support.PARENT_ACTIVITY"
              android:value="com.example.myfirstapp.MainActivity" /&gt;
      &lt;/activity&gt;
          &lt;activity android:name="AddressChange"&gt;&lt;/activity&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><b>Fragment_main</b></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: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.example.app.MainActivity$PlaceholderFragment" &gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><b>Stacktrace</b></p>
  
  <pre><code>06-03 22:30:25.940: E/AndroidRuntime(891): FATAL EXCEPTION: main
  06-03 22:30:25.940: E/AndroidRuntime(891): Process: com.example.app, PID: 891
  06-03 22:30:25.940: E/AndroidRuntime(891): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.DisplayMessageActivity}: java.lang.NullPointerException
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.os.Handler.dispatchMessage(Handler.java:102)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.os.Looper.loop(Looper.java:136)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.main(ActivityThread.java:5042)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at java.lang.reflect.Method.invokeNative(Native Method)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at java.lang.reflect.Method.invoke(Method.java:515)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at dalvik.system.NativeStart.main(Native Method)
  06-03 22:30:25.940: E/AndroidRuntime(891): Caused by: java.lang.NullPointerException
  06-03 22:30:25.940: E/AndroidRuntime(891):  at com.example.app.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:34)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.Activity.performCreate(Activity.java:5231)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
  06-03 22:30:25.940: E/AndroidRuntime(891):  ... 11 more
  </code></pre>