user4757345 user4757345 - 2 months ago 17
Android Question

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.

Answer

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