title: Android AlertDialog Custom View
body: <p>I have searched on Stack Overflow about this topic. However, I couldn't find any similar questions. </p>
  
  <p>I have an alert dialog with a custom layout. When I try to access any view in this custom layout by using <code>findViewById</code> method of alert dialog, I end up with null pointer exception.</p>
  
  <p>The XML layout of custom dialog is this:</p>
  
  <p></p>
  
  <pre><code>    &lt;TextView
          style="@style/TextAppearance.AppCompat.Title"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="@dimen/padding_large"
          android:text="@string/settings" /&gt;
  
      &lt;TextView
          style="@style/TextAppearance.AppCompat.Subhead"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="@dimen/padding_large"
          android:text="Search Distance" /&gt;
  
      &lt;SeekBar
          android:id="@+id/sb_distance"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:max="25"
          android:padding="@dimen/padding_large" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And here is the code for accessing layout:</p>
  
  <pre class="lang-java prettyprint-override"><code>AlertDialog settingsDialog = new AlertDialog.Builder(getActivity()).
                  setTitle(R.string.settings)
                  .setView(R.layout.layout_distance_settings)
                  .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          // Do sth here
                          dialogInterface.dismiss();
                      }
                  })
                  .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          dialogInterface.dismiss();
                      }
                  })
                  .create();
  
          SeekBar distanceSB = (SeekBar) settingsDialog.findViewById(R.id.sb_distance);
          distanceSB.setProgress(5);
          settingsDialog.show();
  </code></pre>
  
  <p>Creating layout by using layout inflater solves problem. However, I want to know what I am doing wrong in here.</p>
  
  <p>Using layout inflater, everything is ok. Here is the code:</p>
  
  <pre class="lang-java prettyprint-override"><code>AlertDialog settingsDialog = new AlertDialog.Builder(this).
                      setTitle(R.string.settings)
                      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              // Do sth here
                              dialogInterface.dismiss();
                          }
                      })
                      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              dialogInterface.dismiss();
                          }
                      })
                      .create();
  
              View parentView = settingsDialog.getLayoutInflater().inflate(R.layout.layout_distance_settings, null);
  
              SeekBar distanceSB = (SeekBar) parentView.findViewById(R.id.sb_distance);
              distanceSB.setProgress(5);
              settingsDialog.setView(parentView);
              settingsDialog.show();
  </code></pre>
  
  <p>So again my question is this: What is the correct way of using <code>setView</code> and how can I access child views of layout used as argument of <code>setView?</code></p>