[android] android.support.design.widget.NavigationView 클래스 확장 오류

Support Design Library 의 새 구성 요소 NavigationView 에 대한 자습서를 따랐는데이 오류 메시지가 표시되지 않습니다.

Error inflating class android.support.design.widget.NavigationView

여기에서 모든 해결 방법을 시도했습니다.

Android 디자인 지원 라이브러리 요소 사용시 오류

그러나 오류 메시지는 여전히 존재합니다.

xml

<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
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <include layout="@layout/toolbar" android:id="@+id/mainToolBar" />

    <fragment android:name="com.ais.cherry.fragment.LoginFragment"
        android:id="@+id/loginFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/mainToolBar" />
    <fragment     android:name="com.ais.cherry.fragment.WaterFallFragment"
        android:id="@+id/mainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/mainToolBar" />
    <fragment android:name="com.ais.cherry.fragment.SearchFragment"
        android:id="@+id/searchFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/mainToolBar" />
    <fragment android:name="com.ais.cherry.fragment.ChatMainFragment"
        android:id="@+id/chatMainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/mainToolBar" />
    <fragment android:name="com.ais.cherry.fragment.ProfileFragment"
        android:id="@+id/profileFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/mainToolBar" />


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:itemTextColor="#212121"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

build.gradle

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.ais.cherry"
    minSdkVersion 16
    targetSdkVersion 22
    multiDexEnabled true
    renderscriptTargetApi 22
    renderscriptSupportModeEnabled true

}
dependencies {
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
}

프로젝트 build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

styles.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">

</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:actionModeStyle">@style/AppTheme.ActionModeStyle</item>
    <item name="android:windowActionBar">false</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowNoTitle">true</item>
    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>

    <!-- ActionBar color -->
    <item name="colorPrimary">#E91E63</item>
    <!-- Status bar color -->
    <item name="colorPrimaryDark">#C2185B</item>
    <!-- Window color -->
    <item name="android:windowBackground">@color/white</item>
    <!-- ActionBar title text -->
    <item name="android:titleTextStyle">@style/AppTheme.MyActionBarTitleText</item>

    <item name="colorAccent">#9E9E9E</item>

    <item name="drawerArrowStyle">@style/AppTheme.MyDrawerArrowStyle</item>

    <!-- color for actionMenu-->
    <item name="actionMenuTextColor">#FFFFFF</item>
    <item name="android:actionMenuTextColor">#FFFFFF</item>
    <!-- android:textColorSecondary is the color of the menu
   overflow icon (three vertical dots) -->
    <item name="android:textColorPrimary">#212121</item>
    <item name="android:textColorSecondary">#FFFFFF</item>
    <!--Navigation bar color-->
    <item name="android:navigationBarColor">#E91E63</item>
    <!--Status bar color-->
    <item name="android:statusBarColor">#C2185B</item>
</style>

어떤 도움을 주시면 감사하겠습니다!

편집-@ menu / drawer 및 @ menu / drawer_header 및 stacktrace 추가

drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_first_fragment"
            android:icon="@drawable/home_view"
            android:checked="true"
            android:title="@string/main"/>
        <item
            android:id="@+id/nav_second_fragment"
            android:icon="@drawable/comment_view"
            android:title="@string/chat"/>
        <item
            android:id="@+id/nav_third_fragment"
            android:icon="@drawable/user_view"
            android:title="@string/profile"/>
        <item android:title="@string/search">
            <menu>
                <item
                    android:title="@string/clothes"/>
                <item
                    android:title="@string/pants"/>
            </menu>
        </item>
    </group>
</menu>

drawer_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="?attr/colorPrimaryDark"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:gravity="bottom">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:textColor="@android:color/white"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>

StackTrace (그 중 일부만)

아래와 같이 세 가지 예외를 던졌습니다. 모두 layout.xml의 “setContentView (layout)”줄에 문제가 발생했음을 지적합니다.

java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.ais.cherry/com.ais.cherry.activity.FirstActivity}:    android.view.InflateException: Binary XML file line #32: Error inflating    class android.support.design.widget.NavigationView

Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class android.support.design.widget.NavigationView

Caused by: java.lang.reflect.InvocationTargetException

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0



답변

실제로 primarycolortext는 종속성을 업그레이드 또는 다운 그레이드하는 문제가 아닙니다.이 문제는 appcompat라이브러리와 디자인 지원 라이브러리 의 버전이 일치하지 않을 때 발생할 수 있습니다 .

조건 일치의 예

compile 'com.android.support:appcompat-v7:23.1.1' // appcompat library
compile 'com.android.support:design:23.1.1'       //design support library


답변

비슷한 오류가 발생했습니다. 내가 사용할 때

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

    <item name="colorPrimary">#673AB7</item>
    <item name="colorPrimaryDark">#512DA8</item>
    <item name="colorAccent">#00BCD4</item>
    <item name="android:textColorPrimary">#212121</item>
    <item name="android:textColorSecondary">#727272</item>

</style>

android : textColorPrimary 및 android : textColorSecondary 테마 항목을 제거하면 나를 위해 작동합니다.

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

    <item name="colorPrimary">#673AB7</item>
    <item name="colorPrimaryDark">#512DA8</item>
    <item name="colorAccent">#00BCD4</item>

</style>

매우 간단한 앱 테마로 작업 해보세요.

편집하다:

튜토리얼 이 도움 이 될 것입니다. 내 이해는 “android : textColorPrimary”를 사용하려면 최소 API 레벨 21이 필요합니다. “android :”없이 동일한 태그를 사용하면 디자인 지원 라이브러리가 사용됩니다. 모든 지원 라이브러리 위젯은 “android : textColorPrimary”대신 “textColorPrimary”항목을 찾으려고 시도하며 동일한 항목을 찾지 못하면 위에서 언급 한 오류가 발생합니다.


답변

나는 또한 같은 오류가 있었다. 제 경우에는 일부 리소스가 drawable-v21에만있었습니다. 해당 리소스도 드로어 블 폴더에 복사하십시오. 이것은 나를 위해 문제를 해결했습니다.

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0 

이것이 주요 문제입니다.


답변

이미로 마이그레이션 한 경우 androidx레이아웃 xml을 다음에서 변경해야합니다.

<android.support.design.widget.NavigationView ... />

…에

<com.google.android.material.navigation.NavigationView ... />


답변

동일한 오류가 발생하여 app:itemTextColor="@color/a_color"내비게이션보기 에 추가 하여 해결했습니다 .

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    app:headerLayout="@layout/layout_drawer_header"
    app:menu="@menu/drawer_menu"
    app:itemTextColor="@color/primary"/>

이 방법으로 android:textColorPrimaryandroid:textColorSecondary테마를 계속 사용할 수 있습니다 .


답변

그래서이 오류를 수정하려고했습니다. 그리고 아무도 나를 위해 일하지 않았습니다. 해결책을 찾을 수 없었습니다.
대본:

Navigation Drawer ProjectAndroid Studio 2.1.2 내부 를 만들려고했는데 에서 기본 Android 아이콘을 변경하려고 할 때 nav_header_main.xml이상한 오류가 발생했습니다. 내 PNG로고를 ...\app\src\main\res\drawable-21. 내 PNG 로고를 ...\app\src\main\res\drawablebam 에 넣으려고 할 때 ! 모든 이상한 오류가 사라집니다.

다음은 PNG를 drawable-21폴더 에 넣을 때 스택 추적 중 일부입니다 .

08-17 17:29:56.237 6644-6678/myAppName  E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
08-17 17:30:01.674 6644-6644/myAppName E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{myAppName.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
                                                                             at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956)
                                                                             at android.app.ActivityThread.access$700(ActivityThread.java:169)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                             at android.os.Looper.loop(Looper.java:194)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5433)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:525)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
                                                                             at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                             at edu.uswat.fwd82.findmedoc.MainActivity.onCreate(MainActivity.java:22)
                                                                             at android.app.Activity.performCreate(Activity.java:5179)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956at android.app.ActivityThread.access$700(ActivityThread.java:169at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394at android.os.Handler.dispatchMessage(Handler.java:107at android.os.Looper.loop(Looper.java:194at android.app.ActivityThread.main(ActivityThread.java:5433at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:525at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691at dalvik.system.NativeStart.main(Native Method) 
                                                                          Caused by: java.lang.reflect.InvocationTargetException
                                                                             at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                             at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                                                                             at android.view.LayoutInflater.createView(LayoutInflater.java:587)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687at android.view.LayoutInflater.rInflate(LayoutInflater.java:746at android.view.LayoutInflater.inflate(LayoutInflater.java:489at android.view.LayoutInflater.inflate(LayoutInflater.java:396at android.view.LayoutInflater.inflate(LayoutInflater.java:352at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140at edu.uswat.fwd82.findmedoc.MainActivity.onCreate(MainActivity.java:22at android.app.Activity.performCreate(Activity.java:5179at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956at android.app.ActivityThread.access$700(ActivityThread.java:169at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394at android.os.Handler.dispatchMessage(Handler.java:107at android.os.Looper.loop(Looper.java:194at android.app.ActivityThread.main(ActivityThread.java:5433at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:525at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691at dalvik.system.NativeStart.main(Native Method) 
                                                                          Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class ImageView
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:189)
at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:262)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:173)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:95)
at java.lang.reflect.Constructor.constructNative(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:417at android.view.LayoutInflater.createView(LayoutInflater.java:587at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687at android.view.LayoutInflater.rInflate(LayoutInflater.java:746at android.view.LayoutInflater.inflate(LayoutInflater.java:489at android.view.LayoutInflater.inflate(LayoutInflater.java:396at android.view.LayoutInflater.inflate(LayoutInflater.java:352at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140at edu.uswat.fwd82.findmedoc.MainActivity.onCreate(MainActivity.java:22at android.app.Activity.performCreate(Activity.java:5179at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956at android.app.ActivityThread.access$700(ActivityThread.java:169at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394at android.os.Handler.dispatchMessage(Handler.java:107at android.os.Looper.loop(Looper.java:194at android.app.ActivityThread.main(ActivityThread.java:5433at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:525at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691at dalvik.system.NativeStart.main(Native Method) 
                                                                          Caused by: java.lang.NullPointerException
                                                                             at android.content.res.ResourcesEx.getThemeDrawable(ResourcesEx.java:459)
                                                                             at android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:435)
                                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:609)
                                                                             at android.widget.ImageView.<init>(ImageView.java:120)
                                                                             at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                             at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
                                                                             at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:

위의 스택 추적에는 다음이 포함됩니다.

android.support.design.widget.NavigationView. (NavigationView. (NavigationView. java : 95)


답변

나는 gradle에서 다운 그레이드를 해결했습니다.

compile 'com.android.support:design:23.1.0'

…에

compile 'com.android.support:design:23.0.1'

Android Studio의 구성 요소를 업데이트하면 항상 문제가 발생하는 것 같습니다. 지겹다.