Android KitKat: android.view.InflateException: 错误膨胀类 android



下面是我的配置。这适用于Android Lollipop,但在Android Kitkat上崩溃。

我在这里尝试了所有解决方法,但无济于事:
膨胀类 android.support.design.widget.NavigationView 时出错

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23
    // Support RenderScript Library v8
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true
}
}
ext{
    supportLibVersion = '23.1.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:${supportLibVersion}"
    compile "com.android.support:design:${supportLibVersion}"
    compile "com.android.support:cardview-v7:${supportLibVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibVersion}"
}

主题style.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

布局activity_main.xml

<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_main"
        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:headerLayout="@layout/nav_header_logged_out_kp"
        app:itemTextColor="@color/textColorPrimary"
        app:menu="@menu/activity_drawer" />
</android.support.v4.widget.DrawerLayout>  

活动oncreate()

setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
navHeaderView = navigationView.getHeaderView(0);  

菜单activity_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_kpv"
            android:icon="@drawable/ic_menu_person"
            android:title="@string/action_kpv" />
    </group>
    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>
</menu>  

这是我得到的错误:

Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.NavigationView
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:627)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
                                                                          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 com.cammob.watershed.kp.ui.KPMainActivity.onCreate(KPMainActivity.java:68)
                                                                          at android.app.Activity.performCreate(Activity.java:5447)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                          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:1268) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                          at 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:423)
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:601)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703) 
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                                                                          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 com.cammob.watershed.kp.ui.KPMainActivity.onCreate(KPMainActivity.java:68) 
                                                                          at android.app.Activity.performCreate(Activity.java:5447) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)  

更新:导航视图的标题nav_header_logged_out_kp.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="@dimen/nav_header_height"
             android:theme="@style/ThemeOverlay.AppCompat.Dark"
    >
    <!--android:background="@drawable/side_nav_bar"-->
    <ImageView
        android:id="@+id/imageBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/login"/>
</FrameLayout>

我找到了。错误来自导航菜单。当我使用导航抽屉创建新项目时,Android Studio 为我生成了完整的工作。有一个名为drawable-v21的可绘制文件夹,它存储图标xml文件,如下所示:

ic_menu_send.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z" />
</vector>  

这是我的错。我错过了为棒棒糖前版本放ic_menu_send.png。

我在Android工作室中使用Activity Gallery来创建navigation drawer activity

然后我注意到该模板added 21 及更高版本的way中的图像 can understand ,这些图像用于menu items navigation view

这是错误

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020052
......
......
android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems

如果您在R文件中跟踪了Resource ID #0x7f020052

你会find它属于安卓没有识别(找到)它的图像,因为它可以被安卓 v21 识别,而不是 19 或更低

因此,为棒棒糖之前的Android设备添加png图像

我也有类似的问题。 经过多天的测试和测试,终于发现了问题所在。 这可能来自您的菜单项图标之一或导航标题。

已通过删除来检查导航视图

app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer

这两个分别行并检查Android显示器是否有错误

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

检查我的线程并回答。 希望你能从中修复它错误膨胀与资源未找到异常

选项 1: 确保文件名匹配-"@mipmap/图标.png"@mipmap/图标.xml"

选项 2: 在 degub 窗口中,您将看到资源 ID 为:r-0X000fd某物....转到 R 文件并检查此资源,因此生成错误....

为此:C:\Users\akshyata\Desktop\FoodSanta_Folders\FoodSanta\app\build\generated \source\r\debug\'your_package_name'\R.java

在记事本中打开 R 文件,然后执行"CTRL+F",然后键入资源 ID。您将找到生成错误的资源...去解决错误

快乐编码

相关内容

  • 没有找到相关文章

最新更新