自定义类扩展FloatingActionButton时出错

时间:2016-09-18 09:55:43

标签: android floating-action-button

我创建了一个自定义类,它扩展了FloatingActionButton

public class customFAB extends FloatingActionButton
{
    public customFAB(Context context)
    {
        super(context);
    }
}

我从MainActivity调用此类

customFAB cFab = new customFAB(getApplicationContext);

但是我收到了这个错误

You need to use a Theme.AppCompat theme (or descendant) with the design library.

我的style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

主题是Theme.AppCompat。那有什么问题?

感谢。

1 个答案:

答案 0 :(得分:3)

尽管您可以使用Application设置应用的默认主题,但Context theme上没有主题。清单中<application>元素的属性。在实例化Activity时,您需要使用Context View

例如:

customFAB cFab = new customFAB(MainActivity.this);