error when custom class extends FloatingActionButt

2019-03-06 02:47发布

I create a custom class and it extends FloatingActionButton

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

I call this class from MainActivity

customFAB cFab = new customFAB(getApplicationContext);

But I get this error

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

my style.xml is

<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 is Theme.AppCompat. so what is the problem ?

Thanks.

1条回答
劳资没心,怎么记你
2楼-- · 2019-03-06 03:30

Your Application's Context will not have a theme on it, despite the fact that you may set your app's default theme by using the theme attribute on the <application> element in your manifest. You need to use your Activity's Context when instantiating Views like that.

For example:

customFAB cFab = new customFAB(MainActivity.this);
查看更多
登录 后发表回答