简体   繁体   English

java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 android.widget.Button

[英]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:当我在RelativeLayout使用拖放按钮时,它显示此错误:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button Blockquote java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 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")

View with ID R.id.btn_dialogs is RelativeLayout , not Button .带有 ID R.id.btn_dialogs视图是RelativeLayout ,而不是Button

So, use:所以,使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText - Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText 造成原因:java.lang.ClassCastException:android.widget.RelativeLayout无法转换为android.widget.TextView - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView Java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.FrameLayout - Java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.RelativeLayout无法转换为android.widget.TabHost - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TabHost java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 原因:java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为com.slidingmenu.lib.SlidingMenu - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.slidingmenu.lib.SlidingMenu java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.Button - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM