title: Add button to a layout programmatically
body: <p>I'm having trouble adding a button to a layout that I've created in XML. Here's what I want to achieve:</p>
  
  <pre><code>//some class
  else {
          startActivity(new Intent(StatisticsScreen.this, ScreenTemperature.class));
  }
  ////
  
  //ScreenTemperatureClass
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      //this is where I call another class that
      //displays a nice graph
      setContentView(new GraphTemperature(getApplicationContext()));
  
  }
  </code></pre>
  
  <p>I want to add a <code>Button</code> to this new screen so that it'll appear below the graph.
  I've tried creating a <code>LinearLayout</code> view, then create a <code>Button</code> and add it to this view but I just get <code>NullPointerException</code>s..</p>
  
  <p>Any help would be appreciated. Thanks</p>
  
  <p><strong>EDIT#1</strong></p>
  
  <p>Here's what I've tried using that created a <code>NullPointerException</code> and 'force close':</p>
  
  <pre><code>Button buybutton;
  LinearLayout layout;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(new GraphTemperature(getApplicationContext()));
  
      layout = (LinearLayout) findViewById(R.id.statsviewlayout);
      Button buyButton = new Button(this);
      buyButton.setText(R.string.button_back);
      buyButton.setLayoutParams(new LayoutParams(
          ViewGroup.LayoutParams.WRAP_CONTENT,
              ViewGroup.LayoutParams.WRAP_CONTENT));
      layout.addView(buyButton);
  
  }
  </code></pre>
  
  <p>And here's the logcat error:</p>
  
  <pre><code>ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weatherapp/com.weatherapp.ScreenTemperature}: java.lang.NullPointerException
  ERROR/AndroidRuntime(293):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  ERROR/AndroidRuntime(293):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  ERROR/AndroidRuntime(293):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  ERROR/AndroidRuntime(293):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  </code></pre>
  
  <p>theres abviously more lines to do with this error in logcat, not sure if you want it?</p>
  
  <p><strong>EDIT#2</strong></p>
  
  <p>So i tried bhups method:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      GraphTemperature GT = new GraphTemperature(getApplicationContext());             
      layout = (LinearLayout) findViewById(R.id.statsviewlayout);
      Button buyButton = new Button(this);
      buyButton.setText(R.string.button_back);
      buyButton.setLayoutParams(new LayoutParams(
          ViewGroup.LayoutParams.WRAP_CONTENT,
              ViewGroup.LayoutParams.WRAP_CONTENT));
      layout.addView(GT); // line 27
      layout.addView(buyButton);       
      setContentView(layout);           
  }
  </code></pre>
  
  <p>This method produced the same logcat error as above, <code>NullPointerException</code>, indicating it was something to do with line no. 27 which is the <code>layout.addView</code> line of code. Any ideas? Thanks again</p>