Search code examples
androidkotlincastingandroid-textinputedittext

MaterialTextView cannot be cast to com.google.android.material.textfield.TextInputEditText


I realize this is a fairly generic error message with a likely simple fix, but I’m missing something and could use another pair of eyes:

2020-09-25 17:09:29.006 ... E/AndroidRuntime: FATAL EXCEPTION: main
    ...
    java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to com.google.android.material.textfield.TextInputEditText
        at ...MessagesFragment$onViewCreated$1$$special$$inlined$apply$lambda$1.onClick(MessagesFragment.kt:171)
        at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Related code from my fragment:

fab.setOnClickListener {
    MaterialAlertDialogBuilder(it.context).apply {
        setView(R.layout.dialog_messages)
        setPositiveButton("Add") { dialog, which ->
            Log.d(TAG, "Message is ${message.text.toString()}!")
        }
        setNegativeButton("Remove") { dialog, which ->

        }
        setNeutralButton("Cancel", null)
    }.show()
}

The crash is pointing to the following line:

Log.d(TAG, "Message is ${message.text.toString()}!")

Layout for the dialog/setView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Message" >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

A swift clue stick is appreciated!


Solution

  • The problem derives from accessing the wrong view using the synthetics of kotlin. Indeed, the message refers to another view, not the one inside dialog_messages.

    The simplest way is to inflate the dialog's view first, to be able to access its containing views (e.g. dialogView.message.text.toString()), then use the setView(view: View) function instead of setView(layoutId: Int):

    fab.setOnClickListener {
        MaterialAlertDialogBuilder(it.context).apply {
            val dialogView = LayoutInflater.from(it.context).inflate(R.layout.dialog_messages, null, false)
            setView(dialogView)
    
            setPositiveButton("Add") { dialog, which ->
                Log.d(TAG, "Message is ${dialogView.message.text.toString()}!")
            }
            setNegativeButton("Remove") { dialog, which ->
    
            }
            setNeutralButton("Cancel", null)
        }.show()
    }