首页 > 解决方案 > 我正在使用 gowong/material-sheet-fab 但它显示错误。有人可以帮我吗?

问题描述

这是我的 Fab 活动

public class Fab extends FloatingActionButton implements AnimatedFab {

public Fab(Context context) {
    super(context);
}


@Override
public void show() {
    show(0, 0);
}
@SuppressLint("RestrictedApi")
@Override
public void show(float translationX, float translationY) {

    setVisibility(View.VISIBLE);
}

/**
 * Hides the FAB.
 */
@SuppressLint("RestrictedApi")
@Override
public void hide() {

    setVisibility(View.INVISIBLE);
}

}

这是我的主要活动

 // MATERIAL ANIMATED FAB
    Fab fab = findViewById(R.id.fab);
    View sheetView = findViewById(R.id.fab_sheet);
    View overlay = findViewById(R.id.overlay);
    int sheetColor = getResources().getColor(R.color.fab_sheet_color);
    int fabColor = getResources().getColor(R.color.fab_color);

    // Initialize material sheet FAB
    materialSheetFab = new MaterialSheetFab<>(fab, sheetView, overlay,
            sheetColor, fabColor);
    materialSheetFab.setEventListener(new MaterialSheetFabEventListener() {
        @Override
        public void onShowSheet() {
            // Called when the material sheet's "show" animation starts.

        }

        @Override
        public void onSheetShown() {
            // Called when the material sheet's "show" animation ends.

        }

        @Override
        public void onHideSheet() {
            // Called when the material sheet's "hide" animation starts.
        }

        public void onSheetHidden() {
            // Called when the material sheet's "hide" animation ends.
        }
    });

这是我的日志猫

进程:com.teepe.teepe,PID:29259 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.teepe.teepe/com.teepe.teepe.MainActivity}:java.lang.ClassCastException:android.support.design。 widget.FloatingActionButton 不能在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在 android.app.ActivityThread 上转换为 com.teepe.teepe.Fab .-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper .java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.Method。在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 的 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 调用(本机方法) 原因:java。 lang.ClassCastException: android.support.design.widget.FloatingActionButton 不能在 com.teepe.teepe.MainActivity.onCreate(MainActivity.java:95) 在 android.app.Activity.performCreate( Activity.java:6662) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) ) 在 android.app.ActivityThread$H 的 android.app.ActivityThread.-wrap12(ActivityThread.java)。handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java :6077) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:756)android.internal.os.ZygoteInit.main(ZygoteInit.java:756)android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

标签: android

解决方案


正如错误所说,Caused by: java.lang.ClassCastException: android.support.design.widget.FloatingActionButton cannot be cast to com.teepe.teepe.Fab at com.teepe.teepe.MainActivity.onCreate(MainActivity.java:95)在您的 中MainActivity,检查您使用的库中的这行代码:

<com.gordonwong.materialsheetfab.sample.Fab
       android:id="@+id/fab"
       style="@style/Widget.MaterialSheetFab.Fab"
       android:layout_alignParentBottom="true"
       android:layout_alignParentEnd="true"
       android:layout_alignParentRight="true" />

在已声明您的(最好是)的布局中将您更改<path.to.your.FloatingActionButton为。<path.to.your.FabMainActivityactivity_main.xmlR.id.fab

希望这可以解决您的错误。单击答案旁边的复选标记以将其从灰色切换为填充(如果有)。


推荐阅读