浮动操作按钮投射问题

Floating Action Button cast issue

我收到这个错误:

Caused by: java.lang.ClassCastException: com.github.clans.fab.FloatingActionButton cannot be cast to 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);
        }
    });

}
}

您的 activity 正在从支持库中导入,并且当您在 xml com.github.clans.fab.FloatingActionButton 中使用外部库元素时,导入是 android.support.design.widget.FloatingActionButton。 使用正确的导入,你将不会面临 ClassCastException

确保你的 activity 有

import com.github.clans.fab.FloatingActionButton

而不是

import android.support.design.widget.FloatingActionButton

FloatingActionMenu 类似。 确保您的导入以您正在使用的库的包名称开头。通常人们会因为 Android Studio

自动导入而遇到这个错误

您可以像这样简单地使用 android 的 FAB 本身:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="20dp"
    app:backgroundTint="@color/pink"
    android:src="@mipmap/add_icon"/>

您可以根据需要在上面的小部件中自定义 FAB。并这样称呼它:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

让生活更轻松!

请注意,如果您在调用按钮对象时添加了 Button 而不是 FloatingActionButton。只需将 FloatingActionButton /您的按钮对象名称/.