如何将 NavigationView 与标准 Android 主题一起使用?

How to use NavigationView with standard Android themes?

我尝试过集成 NaviogationView。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NavigationView navigationView = new NavigationView(this);
    setContentView(navigationView);
}

但它抛出 java.lang.RuntimeException: Failed to resolve attribute at index 0 之后我尝试在 AndroidManifest

中添加 android:theme="@style/Theme.AppCompat"
<activity android:theme="@style/Theme.AppCompat" android:name="MyActivity"
                  android:label="@string/app_name">

而且它不会崩溃。有没有使用标准 Android 主题和 NavigationView 的解决方案?

这是一个完整的堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MyActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access0(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:93)
        at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
        at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
        at android.support.design.widget.NavigationView.<init>(NavigationView.java:84)
        at com.example.myapp.MyActivity.onCreate(MyActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:93)
            at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
            at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
            at android.support.design.widget.NavigationView.<init>(NavigationView.java:84)
            at com.example.myapp.MyActivity.onCreate(MyActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 0
        at android.content.res.TypedArray.getDrawable(TypedArray.java:747)
        at android.widget.AbsListView.<init>(AbsListView.java:815)
        at android.widget.ListView.<init>(ListView.java:151)
        at android.widget.ListView.<init>(ListView.java:147)
        at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:39)
        at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:35)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:93)
            at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
            at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
            at android.support.design.widget.NavigationView.<init>(NavigationView.java:84)
            at com.example.myapp.MyActivity.onCreate(MyActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

NavigationView 旨在使用它嵌套在 DrawerLayout 中。请参阅此处 Android Developers Blog 以获取一些片段示例。

好像只有我们有这个例外。 :) 不管怎样,有一些关于这个错误的相关问题。解决方案是使用 NavigationView 兼容主题。

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

然后在 AndroidManifest 中使用该样式,如下所示:

  <application
   ...
   android:theme="@style/Theme.YourApplication">
  <activity