【问题标题】:error when custom class extends FloatingActionButton自定义类扩展 FloatingActionButton 时出错
【发布时间】:2023-03-27 22:07:01
【问题描述】:

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

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

我从 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。那么有什么问题呢?

谢谢。

【问题讨论】:

  • 您的主要活动是否扩展了 AppCompat?
  • @JuliánMartínez 不,MainActivty 扩展了 AppCompatActivity。

标签: android floating-action-button


【解决方案1】:

您的Application 的Context 上不会有主题,尽管您可以通过在清单中的&lt;application&gt; 元素上使用theme 属性来设置应用的默认主题。像这样实例化Views 时,您需要使用Activity 的Context。

例如:

customFAB cFab = new customFAB(MainActivity.this);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多