Search code examples
androidandroid-studiokotlinandroid-alertdialogfindviewbyid

findViewById throws null exception when it is used on Alert Dialog widgets


I have a customised alert dialog layout_dialog.xml, activity_main.xml and MainActivivty.kt i am trying to use findViewById() in MainActivity.kt to get a button and textView from the alert dialog here is a part of my code :

tv7 = findViewById(R.id.tv7)
bt4 = findViewById(R.id.bt4)

But i am getting the following exception:

java.lang.NullPointerException: findViewById(R.id.tv7) must not be null

here is the full error message :

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)

Note : i am getting this messages in debugging mode.

EDIT : here i am sharing the layout_dialog.xml snippet:

<?xml version="1.0" encoding="utf-8"?>
<data />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You lost with score"
        android:background="@color/Red2"
        android:textSize="40sp"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv7"
        android:layout_centerHorizontal="true"
        android:text="Restart"
        android:textSize="20sp"
        android:layout_marginTop="15dp" />

    <TextView
        android:id="@+id/tv8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt4"
        android:layout_centerHorizontal="true"
        android:text="Or watch an Ad and continue with 2 chances"
        android:textSize="18sp"
        android:textColor="@color/Black"
        android:layout_marginTop="15dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv8"
        android:layout_centerHorizontal="true"
        android:text="Watch Ad"
        android:textSize="20sp"
        android:layout_marginTop="15dp" />

</RelativeLayout>

and here is where i used the findViewById() in the main activity:

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() }
}

Solution

  • Replace this:

    tv7 = findViewById(R.id.tv7)
    bt4 = findViewById(R.id.bt4)
    

    With this:

    tv7 = messageBoxView.findViewById(R.id.tv7)
    bt4 = messageBoxView.findViewById(R.id.bt4)
    

    You are inflating a view (the dialog) into your activity with:

    val messageBoxView = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null)
    

    You then need to define tv7 and bt4 views, but they are in the view that you inflated previously. So you need to define them based on the inflated view.