FloatingActionButton导致Activity崩溃

时间:2018-12-11 14:30:53

标签: android floating-action-button

我有一个FloatingActionMenu,里面有两个FloatingActionButton。单击其中一个按钮时,我试图打开一个网站,但是运行homePage时,我的应用程序崩溃了。

这是我的Java代码:

public class homePage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    final String website = "http://www.facebook.com/slgnalinprogrammer";
    FloatingActionButton fabWebsite = findViewById(R.id.fabWebsite);

    fabWebsite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(website));
            startActivity(i);
        }
    });
}

我的布局XML:

<com.getbase.floatingactionbutton.FloatingActionsMenu
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    app:fab_addButtonColorNormal="#008577"
    app:fab_addButtonColorPressed="#00574B"
    app:fab_addButtonPlusIconColor="#ffffff"
    app:fab_labelStyle="@style/custom_fab">
    <com.getbase.floatingactionbutton.FloatingActionButton
        android:onClick="openWebsite"
        android:id="@+id/fabWebsite"
        app:fab_title="Website"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fab_colorNormal="#008577"
        app:fab_addButtonColorPressed="#00574B"
        app:fab_icon="@drawable/websiteicon"
        app:fab_size="mini"/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>

崩溃后的堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{slgnalin.impladent/slgnalin.impladent.homePage}: java.lang.ClassCastException: com.getbase.floatingactionbutton.FloatingActionButton cannot be cast to android.support.design.widget.FloatingActionButton
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.ClassCastException: com.getbase.floatingactionbutton.FloatingActionButton cannot be cast to android.support.design.widget.FloatingActionButton
    at slgnalin.impladent.homePage.onCreate(homePage.java:23)
    at android.app.Activity.performCreate(Activity.java:7183)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

1 个答案:

答案 0 :(得分:2)

您的布局元素是com.getbase.floatingactionbutton.FloatingActionButton,但是堆栈跟踪显示您正在尝试将其强制转换为android.support.design.widget.FloatingActionButton

这表明您在FloatingActionButton中导入了错误的Activity类。您的导入声明应为:

import com.getbase.floatingactionbutton.FloatingActionButton;