【问题标题】:You need to use a Theme.AppCompat theme error, even with theme set to AppCompat您需要使用 Theme.AppCompat 主题错误,即使主题设置为 AppCompat
【发布时间】:2015-12-19 12:28:01
【问题描述】:

我正忙于实现支持库,以便我可以使用AppCompatActivty

据我所知,我把所有东西都放在了正确的地方。但我仍然收到错误消息。

styles.xml

<resources>        
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">            
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>       
</resources>

v21/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>     
</resources>

manifest.xml

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher_on"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

activity.java

public class RegisterActivity extends AppCompatActivity

现在,当我运行我的应用程序并尝试打开 RegisterActivty 时,我收到以下错误。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tuta_me/activities.RegisterActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我一定是在什么地方漏掉了什么。我做错了什么?

【问题讨论】:

    标签: java android xml android-appcompat android-theme


    【解决方案1】:

    添加这个

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

    在您的 v21/styles.xml 文件中。没有它,&lt;style name="AppTheme" parent="AppBaseTheme"&gt; 没有任何意义。

    【讨论】:

    • 不从标准styles.xml扩展AppBaseTheme吗
    • 不,它没有。每个API 级别 的每个styles.xml 文件。
    • 这样做了,但仍然出现错误。我在最初的问题中编辑了 v21 样式块,以反映您添加后的情况。
    • 你能用整个 manifest.xml 文件更新你的问题吗?
    • 贴出来,让我们看看。
    【解决方案2】:

    看看这个,也许有帮助:https://stackoverflow.com/a/53749686/6883143 这个答案看起来像:

    我在迁移到 androidx 时遇到了同样的问题。我已经通过简单地更新来解决这个问题

    implementation 'androidx.appcompat:appcompat:1.0.0'
    

    implementation 'androidx.appcompat:appcompat:1.0.2'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2015-04-25
      • 1970-01-01
      • 2018-11-27
      相关资源
      最近更新 更多