添加android.support.design.widget.NavigationView时,Java android崩溃

时间:2017-02-28 09:21:02

标签: java android

当我在activity_main.xml中添加此代码时:

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

我的应用程序崩溃了。在构建gradle中,我添加了一个依赖项:

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'

当我删除android.support.design.widget.NavigationView时,它正在运行 这是一个日志:

Process: com.smok.maps, PID: 16311
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smok.maps/com.smok.maps.MainActivity}: android.view.InflateException: Binary XML file line #431: Binary XML file line #431: Error inflating class android.support.design.widget.NavigationView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: android.view.InflateException: Binary XML file line #431: Binary XML file line #431: Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)
    at android.app.Activity.setContentView(Activity.java:2387)
    at com.smok.maps.MainActivity.onCreate(MainActivity.java:285)
    at android.app.Activity.performCreate(Activity.java:6876)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
    at android.os.Handler.dispatchMessage(Handler.java:102)?
    at android.os.Looper.loop(Looper.java:158)?
    at android.app.ActivityThread.main(ActivityThread.java:7224)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
 Caused by: android.view.InflateException: Binary XML file line #431: Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.createView(LayoutInflater.java:657)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)?
    at android.app.Activity.setContentView(Activity.java:2387)?
    at com.smok.maps.MainActivity.onCreate(MainActivity.java:285)?
    at android.app.Activity.performCreate(Activity.java:6876)?
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
    at android.os.Handler.dispatchMessage(Handler.java:102)?
    at android.os.Looper.loop(Looper.java:158)?
    at android.app.ActivityThread.main(ActivityThread.java:7224)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at android.view.LayoutInflater.createView(LayoutInflater.java:631)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)?
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)?
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:527)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)?
    at android.app.Activity.setContentView(Activity.java:2387)?
    at com.smok.maps.MainActivity.onCreate(MainActivity.java:285)?
    at android.app.Activity.performCreate(Activity.java:6876)?
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
    at android.os.Handler.dispatchMessage(Handler.java:102)?
    at android.os.Looper.loop(Looper.java:158)?
    at android.app.ActivityThread.main(ActivityThread.java:7224)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
 Caused by: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
    at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:98)
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:161)
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:92)
    at java.lang.reflect.Constructor.newInstance(Native Method)?
    at android.view.LayoutInflater.createView(LayoutInflater.java:631)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)?
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)?
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:527)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)?
    at android.app.Activity.setContentView(Activity.java:2387)?
    at com.smok.maps.MainActivity.onCreate(MainActivity.java:285)?
    at android.app.Activity.performCreate(Activity.java:6876)?
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
    at android.os.Handler.dispatchMessage(Handler.java:102)?
    at android.os.Looper.loop(Looper.java:158)?
    at android.app.ActivityThread.main(ActivityThread.java:7224)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
 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:657)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:98)?
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:161)?
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:92)?
    at java.lang.reflect.Constructor.newInstance(Native Method)?
    at android.view.LayoutInflater.createView(LayoutInflater.java:631)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)?
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)?
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:527)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)?
    at android.app.Activity.setContentView(Activity.java:2387)?
    at com.smok.maps.MainActivity.onCreate(MainActivity.java:285)?
    at android.app.Activity.performCreate(Activity.java:6876)?
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
    at android.os.Handler.dispatchMessage(Handler.java:102)?
    at android.os.Looper.loop(Looper.java:158)?
    at android.app.ActivityThread.main(ActivityThread.java:7224)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at android.view.LayoutInflater.createView(LayoutInflater.java:631)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:98)?
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:161)?
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:92)?
    at java.lang.reflect.Constructor.newInstance(Native Method)?
    at android.view.LayoutInflater.createView(LayoutInflater.java:631)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)?
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)?
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)?
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:527)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)?
    at android.app.Activity.setContentView(Activity.java:2387)?
    at com.smok.maps.MainActivity.onCreate(MainActivity.java:285)?
    at android.app.Activity.performCreate(Activity.java:6876)?
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
    at android.os.Handler.dispatchMessage(Handler.java:102)?
    at android.os.Looper.loop(Looper.java:158)?
    at android.app.ActivityThread.main(ActivityThread.java:7224)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 0: TypedValue{t=0x2/d=0x7f0100b8 a=-1}
    at android.content.res.TypedArray.getDrawable(TypedArray.java:883)
    at android.widget.AbsListView.<init>(AbsListView.java:1168)
    at android.widget.ListView.<init>(ListView.java:

nav_header:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
    android:gravity="bottom">

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:id="@+id/imageView" />

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Andy Point"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="andypoint@androidtutorialpoint.com"
        android:id="@+id/textView" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

根据你的错误,它非常清楚它没有膨胀导航菜单。请提供@menu/activity_main_drawer文件。并重新检查外部Stringsdrawablesclosed tags

那么你应该使用

<menu xmlns:android="http://schemas.android.com/apk/res/android">

而不是

<menu xmlns:android="schemas.android.com/apk/res/android">

然后删除;

答案 1 :(得分:0)

这是layout_gravity第431行的问题请尝试使用layout_gravity = left

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:fitsSystemWindows="true" />