自定义类扩展FloatingActionButton时发生错误 [英] error when custom class extends FloatingActionButton

查看:62
本文介绍了自定义类扩展FloatingActionButton时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create a custom class and it extends FloatingActionButton

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

我从MainActivity调用此类

I call this class from 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.那是什么问题呢?

theme is Theme.AppCompat. so what is the problem ?

谢谢.

推荐答案

您的ApplicationContext上将没有主题,尽管您可以通过使用<application>元素上的>属性.像这样实例化View时,需要使用ActivityContext.

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.

例如:

customFAB cFab = new customFAB(MainActivity.this);

这篇关于自定义类扩展FloatingActionButton时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆