没有AppCompat主题的导航抽屉

时间:2017-12-08 13:23:22

标签: android xml navigation-drawer

我正在尝试设置导航抽屉"没有"程序兼容性。我收到错误You need to use a Theme.AppCompat theme (or descendant) with the design library 有什么想法吗?

我将`styles.xml设置为:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="@android:style/Theme.Material.Light">
        <!-- Customize your theme here. -->
    </style>
</resources>

在我的AndroidManifest.xml文件中,我只有android:theme="@style/AppTheme"

我的activity_main.xml有:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

    </RelativeLayout>
    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

nav_header_main和activity_main抽屉内容与android studio示例相同,我的MainActivity onCreate函数非常简单:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

我的build.bradle依赖项是:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-v13:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

错误是:

12-08 15:11:19.095 16936-16936/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.xxx.xxx, PID: 16936
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                       at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                       at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                       at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
                                                       at android.app.Activity.setContentView(Activity.java:2415)
                                                       at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:24)
                                                       at android.app.Activity.performCreate(Activity.java:6684)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                    Caused by: 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:33)
                                                       at android.support.design.widget.NavigationView.<init>(NavigationView.java:104)
                                                       at android.support.design.widget.NavigationView.<init>(NavigationView.java:98)
                                                       at java.lang.reflect.Constructor.newInstance0(Native Method) 
                                                       at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) 
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) 
                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
                                                       at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412) 
                                                       at android.app.Activity.setContentView(Activity.java:2415) 
                                                       at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:24) 
                                                       at android.app.Activity.performCreate(Activity.java:6684) 
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637) 
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) 
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:154) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6186) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

2 个答案:

答案 0 :(得分:2)

implementation 'com.android.support:design:26.1.0'

您从此工件中提取的任何内容都需要appcompat-v7Theme.AppCompat等。在您的情况下,NavigationView来自此库。

my cross-port of the Design widgets to Theme.Material中没有NavigationView。 AFAIK,您需要删除此小部件并使用其他内容作为抽屉内容(例如RecyclerView)。

请注意,DrawerLayout 需要appcompat-v7,因此该部分没问题。

答案 1 :(得分:0)

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">#873AB0</item>
    <item name="colorPrimaryDark">#256DA8</item>
    <item name="colorAccent">#11BCD4</item>
    <item name="android:textColorPrimary">#212222</item>
    <item name="android:textColorSecondary">#646464</item>

</style>

从上面的代码段移除

        <item name="android:textColorPrimary">#212222</item>
        <item name="android:textColorSecondary">#646464</item>

检查Example

希望这会对你有所帮助。