0

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")

1 Answer 1

3

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

So, use:

RelativeLayout btn_dialogs = (RelativeLayout) findViewById(R.id.btn_dialogs);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.