【问题标题】:Material Design support below Lollipop - crashesLollipop 下的 Material Design 支持 - 崩溃
【发布时间】:2015-02-17 20:45:05
【问题描述】:

我一直在尝试实现 Material Design 主题,遵循these instructions

  • 我没有使用ToolBar(必须吗?)
  • 我的所有活动都扩展了 ActionBarActivity。
  • 在整个项目中使用getSupportActionBar()
  • 我正在 gradle 中编译和定位到 API 21(最低要求是 API 15)。
  • 我的<application> 标签包含android:theme="@style/AppTheme"
  • 在 Lollipop 设备上运行应用程序(具有特定的 v21 类似主题的作品)。

我的样式.xml:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
     <item name="displayOptions">useLogo|showHome</item>
    <item name="logo">@drawable/home_page_logo</item>
    <item name="background">@color/actionbar_background_color</item>
    <item name="textColor">@color/white</item>
    <item name="titleTextStyle">@style/MyActionBarTextStyle</item>
</style>

无论我尝试了什么,当我在onCreate() 上启动我的主要活动时,应用程序就会崩溃,并带有此崩溃日志:

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
 at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
 at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)

有人遇到过这个问题吗?关于可能导致此问题的任何建议?

编辑: 这绝对是我的 styles.xml 主题中的东西。如果我强制应用程序使用默认的 Theme.AppCompat 主题,它可以工作。 什么可能导致主题失败?我验证了 ActionBar 属性没有使用“android:”。还要别的吗?

【问题讨论】:

  • @PearsonArtPhoto 是的...再次验证。所有模块都针对 v21。
  • @PearsonArtPhoto 清理、重建、gradle 同步...基本上一切

标签: android android-theme android-appcompat material-design


【解决方案1】:

已解决...

我的 2 个 jar 库显然 生成 values.xml,其中包含 AppThemeAppBaseTheme 的样式。 我只验证了我们的依赖模块,因为 jar 库不应该声明应用程序主题,特别是不使用默认主题的名称。

在发布答案之前,我添加到我的AndroidManifest.xml &lt;application&gt; tools:replace="android:theme" 并声明了新主题,假设它可以工作并且我的应用程序将覆盖任何其他主题。

解决方案最终,虽然很愚蠢,但将我自己的 AppThemeAppBaseTheme 重命名为不同的名称,现在它可以工作了。 在这样一个微不足道的修复上花费了几个小时。希望这会为其他人腾出一些时间。

【讨论】:

    【解决方案2】:

    parent 应该是 Theme.AppCompat 而不是 @style/Theme.AppCompat

    使用@style/,您将使用来自Android API 21 的样式,并且它必须是来自AppCompat 库的样式。

    编辑:

    Widget.AppCompat 可能同样的事情

    edit2:

    喜欢这个

    <style name="AppBaseTheme" parent="Theme.AppCompat">
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    
    </style>
    
    <style name="MyActionBar" parent="Widget.AppCompat.ActionBar.Solid">
         <item name="displayOptions">useLogo|showHome</item>
        <item name="logo">@drawable/home_page_logo</item>
        <item name="background">@color/actionbar_background_color</item>
        <item name="textColor">@color/white</item>
        <item name="titleTextStyle">@style/MyActionBarTextStyle</item>
    </style>
    

    【讨论】:

    • 谢谢。也为所有父母尝试过。
    • 这太奇怪了。明天上班我去看看我们的应用怎么样
    • 很奇怪...我开始认为它与生成带有 Holo 主题的 values.xml 的坏 jar 库有关。
    • 你试过用Theme.Material.AppCompat吗(我只是根据我的记忆写,可能会略有不同,但我记得有这样的东西。)
    【解决方案3】:

    尝试从 styles.xml 的第一行中删除“@style/”,这样你就有了:

    <style name="AppBaseTheme" parent="Theme.AppCompat">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 2021-05-29
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多