title: findViewById throws null exception when it is used on Alert Dialog widgets
body: <p>I have a customised alert dialog <code>layout_dialog.xml</code>, <code>activity_main.xml</code> and <code>MainActivivty.kt</code> i am trying to use <code>findViewById()</code> in <code>MainActivity.kt</code> to get a button and textView from the alert dialog here is a part of my code :</p>
  <pre><code>tv7 = findViewById(R.id.tv7)
  bt4 = findViewById(R.id.bt4)
  </code></pre>
  <p>But i am getting the following exception:</p>
  <pre><code>java.lang.NullPointerException: findViewById(R.id.tv7) must not be null
  </code></pre>
  <p>here is the full error message :</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.wordschain, PID: 8398
  java.lang.NullPointerException: findViewById(R.id.tv7) must not be null
      at com.example.wordschain.MainActivity.create_Alert_Dialog(MainActivity.kt:83)
      at com.example.wordschain.MainActivity.do1(MainActivity.kt:318)
      at com.example.wordschain.MainActivity$onCreate$1.onClick(MainActivity.kt:48)
      at android.view.View.performClick(View.java:6329)
      at android.view.View$PerformClick.run(View.java:25002)
      at android.os.Handler.handleCallback(Handler.java:809)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:166)
      at android.app.ActivityThread.main(ActivityThread.java:7555)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
  </code></pre>
  <p>Note : i am getting this messages in debugging mode.</p>
  <p>EDIT : here i am sharing the <code>layout_dialog.xml</code> snippet:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  </code></pre>
  
  <pre><code>&lt;data /&gt;
  
  &lt;RelativeLayout
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:padding=&quot;16dp&quot;
      android:background=&quot;@color/white&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/tv7&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;You lost with score&quot;
          android:background=&quot;@color/Red2&quot;
          android:textSize=&quot;40sp&quot;
          android:layout_centerHorizontal=&quot;true&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/bt4&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@id/tv7&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:text=&quot;Restart&quot;
          android:textSize=&quot;20sp&quot;
          android:layout_marginTop=&quot;15dp&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/tv8&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@id/bt4&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:text=&quot;Or watch an Ad and continue with 2 chances&quot;
          android:textSize=&quot;18sp&quot;
          android:textColor=&quot;@color/Black&quot;
          android:layout_marginTop=&quot;15dp&quot; /&gt;
  
      &lt;Button
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@id/tv8&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:text=&quot;Watch Ad&quot;
          android:textSize=&quot;20sp&quot;
          android:layout_marginTop=&quot;15dp&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and here is where i used the <code>findViewById()</code> in the main activity:</p>
  <pre><code>class MainActivity : AppCompatActivity() {
      //Some code ....
      override fun onCreate(savedInstanceState: Bundle?) {
          //Some code ...
          }
      fun create_Alert_Dialog(How: Int){
      //Alert dialog builder
      val messageBoxView = LayoutInflater.from(this).inflate(R.layout.layout_dialog,null)
      //Alert dialog builder
      val messageBoxBuilder = AlertDialog.Builder(this).setView(messageBoxView)
      //Setting undissmissable
      messageBoxBuilder.setCancelable(false)
      //Show
      messageBoxBuilder.show()
      tv7 = findViewById(R.id.tv7)
      bt4 = findViewById(R.id.bt4)
      bt4.setOnClickListener { Lose() }
  }
  </code></pre>