title: How to get reference to a Button created in a custom Dialog, using a xml layout?
body: <p>I have an Activity in which user can update a specific information clicking in a button near a label. This buttons than triggers a Dialog where I have some fields to get user input and a button to finish editing. </p>
  
  <p>My problem is that I am not able to get a reference to the button declare in the dialog specific xml layout. The button reference returns null. Follow some code snippet to ilustrate.</p>
  
  <p>Button which fires the event to build the dialog is declared as a instance variable in the activity as follows: </p>
  
  <pre><code>private Button bConfigurarCarro;
  </code></pre>
  
  <p>than onCreate method:</p>
  
  <pre><code>bConfigurarCarro = (Button)findViewById(R.id.bConfigurarCarro);
   bConfigurarCarro.setOnClickListener(configuraCarroListener);
  </code></pre>
  
  <p>this correctly fires the event to create the dialog: </p>
  
  <pre><code>protected OnClickListener configuraCarroListener = new OnClickListener(){
  public void onClick(View v) {
  showDialog(CARRO_DIALOG_ID);
  Log.d(TAG, "Executando evento do botão de configuração de carro no abastecimento.");
              }
          };
  </code></pre>
  
  <p>than to create the dialog Overrides onCreateDialog method like this: </p>
  
  <pre><code>@Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
      case TIME_DIALOG_ID:
          return new TimePickerDialog(this, mTimeSetListener, hora, minuto, false);
      case DATE_DIALOG_ID:
          return new DatePickerDialog(this, mDateSetListener, ano, mes, dia);
      case CARRO_DIALOG_ID:
          Log.d(TAG, "Criando dialog de cadastro de carro.");
          dialogCarro = new Dialog(this);
          dialogCarro.setContentView(R.layout.novo_carro_dialog);
          bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
          bSalvarCarro.setOnClickListener(salvarCarroListener);
          dialogCarro.setTitle("Carro");
          dialogCarro.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
          Log.d(TAG, "Dialog de cadastro de carro criado retornando...");
          return dialogCarro;
      }
          return null;
  }
  </code></pre>
  
  <p>The specific line where the NullPointer triggers is the one where after trying to get Button reference above, I than try to setOnClickListener..</p>
  
  <pre><code>bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
  bSalvarCarro.setOnClickListener(salvarCarroListener);
  </code></pre>
  
  <p>the bSalvarCarro is null.</p>
  
  <p>The xml layout for the Dialog which I try to set above using the line of code: </p>
  
  <pre><code>dialogCarro.setContentView(R.layout.novo_carro_dialog);
  </code></pre>
  
  <p>Is this one(novo_carro_dialog.xml):</p>
  
  <pre><code>&lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" android:layout_height="fill_parent"&gt;
              &lt;TableRow&gt;
                  &lt;TextView android:id="@+id/marcaCarro"
                      android:layout_width="wrap_content" android:layout_height="wrap_content" 
                      android:text="@string/marcaCarro"/&gt;
                  &lt;EditText android:id="@+id/tMarcaCarro" android:singleLine="true" android:layout_width="fill_parent"
                  android:layout_height="wrap_content" android:width="130px"/&gt;
              &lt;/TableRow&gt;
              &lt;TableRow&gt;
                  &lt;TextView android:id="@+id/nomeCarro"
                      android:layout_width="wrap_content" android:layout_height="wrap_content" 
                      android:text="@string/nomeCarro"/&gt;
                  &lt;EditText android:id="@+id/tNomeCarro" android:singleLine="true" android:layout_width="fill_parent"
                  android:layout_height="wrap_content"/&gt;
              &lt;/TableRow&gt;
              &lt;TableRow&gt;
                  &lt;TextView android:id="@+id/anoCarro"
                      android:layout_width="wrap_content" android:layout_height="wrap_content" 
                      android:text="@string/anoCarro"/&gt;
                  &lt;EditText android:id="@+id/tAnoCarro" android:singleLine="true" android:layout_width="fill_parent"
                  android:numeric="integer" android:maxLength="4" android:layout_height="wrap_content" /&gt;
              &lt;/TableRow&gt;
              &lt;TableRow&gt;
                  &lt;TextView android:id="@+id/modeloCarro"
                      android:layout_width="wrap_content" android:layout_height="wrap_content" 
                      android:text="@string/modeloCarro"/&gt;
                  &lt;EditText android:id="@+id/tModeloCarro" android:singleLine="true" android:layout_width="fill_parent"
                  android:layout_height="wrap_content" /&gt;
              &lt;/TableRow&gt;
              &lt;Button android:id="@+id/botaoSalvarCarro" android:layout_width="wrap_content"
                      android:layout_height="wrap_content" android:text="@string/bSalvarCarro" /&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>As you can see the Button is declared with the id botaoSalvarCarro, but trying to get a reference to it returns null. I am a bit confused by this as if I take out the line which sets the listener the dialog is correctly showed, so. How do I get the reference to this button correctly?</p>