二进制XML文件行#16:错误膨胀类android.support.design.widget.NavigationView

时间:2016-02-27 18:30:31

标签: android android-studio

当我将我的应用部署到Android版本低于Android 5.0的设备时,我遇到了此错误。我使用Android Studio Project Assistent创建了应用程序,并说minSdkVersion = 14因此它应该是兼容的。你能救我吗?

摇篮:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "opunktschmidt.weightcontrol"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'joda-time:joda-time:2.8.2'
    compile 'org.apache.commons:commons-lang3:3.4'
}

错误:

03-12 02:15:15.141 27894-27894/opunktschmidt.weightcontrol E/AndroidRuntime:
FATAL EXCEPTION: main
Process: opunktschmidt.weightcontrol, PID: 27894
java.lang.RuntimeException: Unable to start activity ComponentInfo{opunktschmidt.weightcontrol/opunktschmidt.weightcontrol.UI.CalorieDiaryActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
    at android.app.ActivityThread.access$900(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
    at opunktschmidt.weightcontrol.UI.CalorieDiaryActivity.onCreate(CalorieDiaryActivity.java:52)
    at android.app.Activity.performCreate(Activity.java:5451)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$900(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException

XML文件包含NavigationView:

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_caloriediary"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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:itemTextColor="?android:textColorPrimary"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

3 个答案:

答案 0 :(得分:1)

找到问题的原因:Android KitKat: android.view.InflateException: Error inflating class android.support.design.widget.NavigationView

我不明白为什么Android Studio允许您创建与Android 4.0不兼容的Android 4.0兼容项目。 -.-你必须调查才能使它与Android 4.0兼容(对不起英语不好)。

答案 1 :(得分:1)

问题是,您的项目缺少pre-lollipop版本的栅格资源。 如果您有文件values/drawables.xml,请检查。看起来你复制了一些来源,但不是这个文件。该文件包含对光栅图像的引用,用作矢量图像的替代:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="ic_menu_camera" type="drawable">@android:drawable/ic_menu_camera</item>
<item name="ic_menu_gallery" type="drawable">@android:drawable/ic_menu_gallery</item>
<item name="ic_menu_slideshow" type="drawable">@android:drawable/ic_menu_slideshow</item>
<item name="ic_menu_manage" type="drawable">@android:drawable/ic_menu_manage</item>
<item name="ic_menu_share" type="drawable">@android:drawable/ic_menu_share</item>
<item name="ic_menu_send" type="drawable">@android:drawable/ic_menu_send</item>
</resources>

答案 2 :(得分:0)

在堆栈溢出时尝试了所有解决方案,没有任何效果。

问题是:我在 values 文件夹中只有一个 dimens.xml(v21)文件。我的应用程序可以在棒棒糖及更高版本上使用,但在Android 4及更低版本上会崩溃。

Since I create a new *dimens.xml* and copy paste all the attributes from 
dimens.xml(v21) to the new dimens.xml.

现在,该应用可以在Android 4及以下版本上运行。