[Solved] ViewBinding Error: java.lang.ClassCastException: android.widget.RelativeLayout cannot be

prompt: after the article is written, the directory can be automatically generated. For how to generate it, please refer to the help document on the right </ font> for details

Java.lang.classcastexception encountered when using viewbinding: android.widget.relativelayout cannot be cast to cast to android.widget.textview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/back"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Back" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Title"
        android:textSize="20sp" />

    <Button
        android:id="@+id/done"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Done" />

</RelativeLayout>

In the XML of the title, the ID of textview is title

<include
        android:id="@+id/title"
        layout="@layout/titlebar" />
        

Use include < in the main interface>  When introducing a layout, the layout ID set is also the title,
when using viewbinding, there will be confusion, so the ID name should be set to different.


Read More: