M Azam Khan
M Azam Khan

Reputation: 380

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button

When I use the drag & drop button in RelativeLayout, it shows this error:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button Blockquote

       Button btn_dialogs =(Button) findViewById(R.id.btn_dialogs);



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/btn_dialogs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 <Button
        android:id="@+id/button"
        android:layout_width="199dp"
        android:layout_height="62dp"
        android:layout_above="@+id/text_view_countdown"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="125dp"
        android:layout_marginTop="122dp"
        android:layout_marginEnd="87dp"
        android:layout_marginBottom="141dp"
        android:text="btn_dialogs" />

Did anyone has a solution without using:

 @SuppressLint("WrongViewCast")

Upvotes: 0

Views: 143

Answers (1)

S-Sh
S-Sh

Reputation: 3873

View with ID R.id.btn_dialogs is RelativeLayout, not Button.

So, use:

RelativeLayout btn_dialogs = (RelativeLayout) findViewById(R.id.btn_dialogs);

Upvotes: 3

Related Questions