Lollipop 下方的 Material Design 支持 - 崩溃

问题描述 投票:0回答:4

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

  • 我没有使用
    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:”。还要别的吗?

android android-theme android-appcompat material-design
4个回答
11
投票

已解决...

2 我的 jar 库显然有 generated

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

在发布答案之前,我添加到我的

AndroidManifest.xml
<application>
tools:replace="android:theme"
并宣布新主题,假设它会工作并且我的应用程序将覆盖任何其他主题。

solution 最终,尽管它很愚蠢,但它是将我自己的

AppTheme
AppBaseTheme
重命名为不同的名称,现在它起作用了。 在这样一个微不足道的修复上花费了数小时。希望这会为其他人腾出一些时间。


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>

1
投票

尝试从 styles.xml 的第一行中删除“@style/”,这样您就可以:

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

1
投票

就我而言,我的 themes.xml 具有以下内容:

style name="Theme.DollarBucket" parent="android:Theme.Material.Light.NoActionBar" />

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

© www.soinside.com 2019 - 2024. All rights reserved.