簡體   English   中英

java.lang.ClassCastException: android.widget.RelativeLayout 不能轉換為 android.widget.Button

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

當我在RelativeLayout使用拖放按鈕時,它顯示此錯誤:

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

有沒有人有沒有使用的解決方案:

 @SuppressLint("WrongViewCast")

帶有 ID R.id.btn_dialogs視圖是RelativeLayout ,而不是Button

所以,使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM