Search code examples
androidandroid-edittextandroid-appbarlayout

com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText


putting an EditText in an AppBarLayout is throwing an exception. Here my layout.

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        android:id="@+id/toolbar_included"
        layout="@layout/toolbar"
        app:layout_scrollFlags="scroll|enterAlways" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        app:adSize="BANNER"
        app:adUnitId="@string/adUnitIdTest_Banner"
        app:layout_scrollFlags="noScroll" />

    <EditText
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true"
        android:hint="@string/title_hint"
        android:importantForAutofill="no"
        android:inputType="textCapWords"
        android:selectAllOnFocus="true"
        android:textSize="15sp"
        app:fontFamily="@font/raleway_regular" />

</com.google.android.material.appbar.AppBarLayout>

I am getting the following exception when starting the activity:

Caused by: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText

viewBiding is true. I don't know which MaterialTextView the exception is referring to.

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityWorkoutBinding.inflate(layoutInflater)
    setContentView(binding.root)

The line binding = ActivityWorkoutBinding.inflate(layoutInflater) is throwing the exception.

Best regards


Solution

  • I just figured it out. The AppBarLayout seems to have a built in TextView with the id title. I changed the id from my EditText from title to something else and it worked.