【问题标题】:Android "You need to use a Theme.AppCompat theme (or descendant) with the design library"Android“您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用”
【发布时间】:2017-03-17 16:20:45
【问题描述】:

好的,我收到此错误(标题)。 许多帖子建议我(甚至是Android工作室)将主题更改为 App.Appcompat ,我已经做到了。

清单文件:

 <uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat">
    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".activity.ContactAcitivty"
        android:label="@string/activity_title_contact" />
    <activity
        android:name=".activity.CopyrightActivity"
        android:label="@string/activity_title_copyright" />
    <activity android:name=".activity.FormActivity">
            <intent-filter>
                <action android:name="info.androidhive.navigationdrawer.activity.FormActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

</application>

样式.xml

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

但是,没有任何改变。

我不知道发生了什么,因为我之前没有遇到过这个问题(我之前没有在清单文件中添加 App.compat)

分级:

compile 'com.android.support:appcompat-v7:24.0.0-beta1'
compile 'com.android.support:design:24.0.0-beta1'
compile 'com.android.support:support-v4:24.0.0-beta1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support.constraint:constraint-layout:+'
testCompile 'junit:junit:4.12'

错误日志:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme     (or descendant) with the design library.    
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)     
at android.support.design.widget.NavigationView.<init> (NavigationView.java:102)   
at android.support.design.widget.NavigationView.<init>(NavigationView.java:96)   
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)         at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)           

at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)  
at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)      
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)

 

感谢您的帮助

【问题讨论】:

  • 发布完整清单...
  • 为什么要使用测试版的支持库?
  • 好的,我编辑了我的帖子。我只是从 androidhive.info/2013/11/… 关注关于导航抽屉的教程,所以我使用了教程中解释的 beta 版本
  • '24.0.0-beta1' 是旧的测试版。当前稳定版本是'25.2.0'
  • 好的,我会改变版本,并告诉你是否能解决我的问题

标签: android android-layout android-appcompat android-theme


【解决方案1】:

试试这个改变:

在毕业典礼中:

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:design:25.2.0'

同样在清单中将appcompat 主题添加到您的导航抽屉`Activity

<activity android:name=".activity.FormActivity"
           android:theme="@style/Theme.AppCompat">
            <intent-filter>
                <action android:name="info.androidhive.navigationdrawer.activity.FormActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

【讨论】:

    【解决方案2】:

    您的MainActivity 使用的是@style/AppTheme.NoActionBar 主题,它不是Theme.AppCompat 的后代

    改变

    &lt;style name="AppTheme.NoActionBar"&gt;

    &lt;style name="AppTheme.NoActionBar" parent="AppTheme"&gt;

    【讨论】:

      【解决方案3】:

      如果您使用约束布局,请确保您使用的版本大于或等于 1.1.3。我在使用 1.0.2 版本时遇到此错误

      【讨论】:

        猜你喜欢
        • 2016-02-17
        • 2016-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        相关资源
        最近更新 更多