user4757345 user4757345 - 10 months ago 78
Android Question

error when custom class extends FloatingActionButton

I create a custom class and it extends


public class customFAB extends FloatingActionButton
public customFAB(Context 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.


<!-- 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 name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

theme is
. so what is the problem ?


Answer Source

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