title: Can I use setContentView outside the oncreate method?
body: <p>I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:</p>
  
  <p>AlarmActivity.java:</p>
  
  <pre><code>package com.alarm.example;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.Button;
  import android.widget.RelativeLayout;
  
  public class AlarmActivity extends Activity{
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Button buton = new Button(this);
          buton.setId(101);
          buton.setText("New alarm");
          buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
          RelativeLayout layout1 = new RelativeLayout(this);  
          layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
      layout1.addView(buton);  
      setContentView(layout1); 
  
      Button nou =(Button)findViewById(101);
      nou.setOnClickListener(new View.OnClickListener(){
          @Override
          public void onClick(View v){
              New_ala nou1=new New_ala();
              nou1.nou_alarma();
              }
          });
      }
  }
  </code></pre>
  
  <p>New_ala.java:</p>
  
  <pre><code>package com.alarm.example;
  
  public class New_ala extends AlarmActivity{
  public void nou_alarma() {
  setContentView(R.layout.timepicker);        
  }
  }
  </code></pre>
  
  <p>TimePicker.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
  
      &lt;TimePicker
          android:id="@+id/timePicker"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" /&gt;
  
      &lt;LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="fill_parent" 
          android:layout_alignParentBottom="true"&gt;
  
          &lt;Button 
          android:id="@+id/picker_save" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="1" 
          android:text="Save"&gt;
          &lt;/Button&gt;
  
          &lt;Button 
          android:id="@+id/picker_cancel" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"
          android:layout_weight="1" 
          android:text="Cancel"&gt;
          &lt;/Button&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?</p>
  
  <p>The code after setting the intent:
  AlarmActivity:</p>
  
  <pre><code>package com.alarm.example;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  public class AlarmActivity extends Activity{
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          startActivity(new Intent(this, NewAlarmActivity.class));
          }
      }
  </code></pre>
  
  <p>NewAlarmActivity:</p>
  
  <pre><code>package com.alarm.example;
  import android.os.Bundle;
  public class NewAlarmActivity extends AlarmActivity{
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.timepicker);
  }
      }
  </code></pre>