我收到此错误:


  造成原因:
  java.lang.ClassCastException:
  com.github.clans.fab.FloatingActionButton无法转换为
  android.support.design.widget.FloatingActionButton。


这是我的代码:

Vechile.xml

   <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/tools">


   <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/material_design_android_floating_action_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
         android:layout_marginBottom="64dp"
         android:layout_marginRight="16dp"
        fab:menu_animationDelayPerItem="55"
        fab:menu_backgroundColor="@android:color/transparent"
        fab:menu_buttonSpacing="0dp"
        fab:menu_colorNormal="#3F51B5"
        fab:menu_colorPressed="#3F51B5"
        fab:menu_colorRipple="#3F51B5"
        fab:menu_fab_size="normal"
        fab:menu_icon="@drawable/ic_add"
        fab:menu_labels_colorNormal="#3F51B5"
        fab:menu_labels_colorPressed="#3F51B5"
        fab:menu_labels_colorRipple="#66efecec"
        fab:menu_labels_cornerRadius="3dp"
        fab:menu_labels_ellipsize="none"
        fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
        fab:menu_labels_margin="0dp"
        fab:menu_labels_maxLines="-1"
        fab:menu_labels_padding="8dp"
        fab:menu_labels_position="left"
        fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
        fab:menu_labels_showShadow="true"
        fab:menu_labels_singleLine="false"
        fab:menu_labels_textColor="#f2f1f1"
        fab:menu_labels_textSize="15sp"
        fab:menu_openDirection="up"
        fab:menu_shadowColor="#66aff198"
        fab:menu_shadowRadius="4dp"
        fab:menu_shadowXOffset="1dp"
        fab:menu_shadowYOffset="4dp"
        fab:menu_showShadow="true">

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/material_design_floating_action_menu_item1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/car"
            fab:fab_label="Add Car"
            fab:fab_size="mini"
            fab:menu_colorNormal="#3F51B5"/>
       </com.github.clans.fab.FloatingActionMenu>
   </RelativeLayout>


Vechile.java

FloatingActionMenu materialDesignFAM;
FloatingActionButton floatingActionButton1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vehicle);
    materialDesignFAM = (FloatingActionMenu)
    findViewById(R.id.material_design_android_floating_action_menu);
    floatingActionButton1 = (FloatingActionButton)
    findViewById(R.id.material_design_floating_action_menu_item1);

    floatingActionButton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


           Intent intent = new Intent(VehicleActivity.this, Brand
 Activity.class);

            startActivity(intent);
        }
    });

}
}

最佳答案

您的活动是从支持库导入的,而在xml android.support.design.widget.FloatingActionButton中使用外部库元素时,导入为com.github.clans.fab.FloatingActionButton
使用正确的导入,您将不会面对ClassCastException

确保您的活动有

import com.github.clans.fab.FloatingActionButton

并不是

import android.support.design.widget.FloatingActionButton

FloatingActionMenu也是如此。
确保导入从所使用的库的软件包名称开始。通常情况下,由于Android Studio自动导入,人们会遇到这个错误时间

关于android - float Action 按钮转换问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44506821/

10-10 10:03