Material Design在Lollipop以下版本的支持 - 崩溃问题

3

我一直在尝试实现Material Design主题,遵循这些说明

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

我的styles.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版本。 - Lior Iluz
@PearsonArtPhoto 清理、重建、Gradle同步...基本上所有的东西。 - Lior Iluz
4个回答

11

解决方法...

我的两个jar库显然生成了一个包含AppThemeAppBaseTheme样式的values.xml。 我核实了我们的依赖模块,因为jar库不应该声明应用程序主题,尤其不是默认主题的名称。

在发布答案之前,我向我的AndroidManifest.xml添加了<application> tools:replace =“android:theme”,并声明了新的主题,假设它能正常工作并覆盖其他主题。

最终的解决方案,尽管很愚蠢,但是将我的自定义AppThemeAppBaseTheme重命名为不同的名称,现在它可以正常工作了。花费了几个小时来解决一个如此微不足道的问题。希望这能为其他人节省一些时间。


2

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

使用@style/,您将使用Android API 21中的样式,而必须使用AppCompat库中的样式。

编辑:

Widget.AppCompat也可能是同样的情况。

编辑2:

像这样:

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

谢谢。我也尝试过了,对于所有的父级元素都是如此。 - Lior Iluz
明天上班时我会检查我们的应用程序,这很奇怪。 - Budius
非常奇怪...我开始认为这可能与一个生成带有Holo主题的values.xml的糟糕jar库有关。 - Lior Iluz
你尝试使用Theme.Material.AppCompat了吗?(我只是根据我的记忆写的,可能会略有不同,但我记得这样的东西存在。) - Budius

1
尝试从styles.xml的第一行中删除“@style /”,使其变为:
<style name="AppBaseTheme" parent="Theme.AppCompat">

1
在我的情况下,我的themes.xml文件包含以下内容:
style name="Theme.DollarBucket" parent="android:Theme.Material.Light.NoActionBar" />

由于某种奇怪的原因,出现了一个“android:”标签,删除它解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接