utarid
utarid

Reputation: 1715

error when custom class extends FloatingActionButton

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.

Upvotes: 0

Views: 86

Answers (1)

Mike M.
Mike M.

Reputation: 39191

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

Upvotes: 3

Related Questions