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



当我将我的应用程序部署到安卓版本低于安卓5.0的设备时,我收到此错误。我用die Android Studio Project Asistent 创建了这个应用程序,并说 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 文件包含导航视图:

    <?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>

找到问题的原因: Android KitKat: android.view.InflateException: 膨胀类时出错 android.support.design.widget.NavigationView

我不明白为什么Android Studio让你创建一个与Android 4.0不兼容的Android 4.0兼容项目。 -.- 您必须进行调查以使其与Android 4.0兼容(抱歉英语不好)。

问题是,您的工程缺少棒棒糖前版本的栅格资源。检查是否有文件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>

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

问题是:我在 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及更低版本上运行。

最新更新