2017-06-12 105 views
0

我收到此錯誤:浮動操作按鈕鑄的問題

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); 
     } 
    }); 

} 
} 

回答

3

您的活動正在從支持庫導入而當您在xml com.github.clans.fab.FloatingActionButton中使用外部庫元素時,導入爲android.support.design.widget.FloatingActionButton。 使用正確的進口,你會不會面臨ClassCastException

確保您的活動有

import com.github.clans.fab.FloatingActionButton

,而不是

import android.support.design.widget.FloatingActionButton

類似的事情FloatingActionMenu。 確保您的導入從您正在使用的庫的包名稱開始。通常,人們面對這樣的時間,因爲汽車進口的誤差由Android工作室

+0

@NoLuckPalak.Thanks。 – HARDEEP

+0

不客氣! – Pulak

0

您只需使用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" 
    android:visibility="gone"/> 

您可以自定義FAB但是你在上面的小部件本身喜歡。並且這樣稱呼:

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

使生活更輕鬆!

0

看到如果你在調用按鈕對象時添加了Button而不是FloatingActionButton。只需簡單地把FloatingActionButton /你的按鈕對象名稱/。