Android KitKat:android.view.InflateException:膨胀时出错 class android.support.design.widget.NavigationView
Android KitKat: android.view.InflateException: Error inflating class android.support.design.widget.NavigationView
以下是我的配置。这适用于 Android Lollipop 但在 Android Kitkat 上崩溃。
我尝试了这里的所有解决方法,但无济于事:
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>
Activity 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.access0(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.access0(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)
更新: NavigationView 的 header 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 及更高版本 can understand
中的模板 added
图像,navigation view
中的这些图像 menu items
这里是错误
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020052
......
......
android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems
如果您在 R
文件
中追踪 Resource ID #0x7f020052
你会 find
它属于那个 android 没有识别(找到)它的图像,因为它可以被 android v21 识别,而不是 19 或更低
so add png
images for pre-lollipop android devices
我有类似的问题。经过多日测试终于找到问题所在。这可能来自您的菜单项图标之一或导航 header.
已通过删除
检查导航视图
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"
/>
查看我的话题并回答。希望你能解决它
Error Inflate with resourcenotfoundException
选项 1:
确保文件名匹配 -
“@mipmap/icon.png”和“@mipmap/icon.xml”
选项2:
在 degub window 中,您将看到资源 ID 为:r-0X000fd something.... 转到 R 文件并检查此资源到期产生错误的地方.....
这样做:
C:\Users\akshyata\Desktop\FoodSanta_Folders\FoodSanta\app\build\generated\source\r\debug\'your_package_name'\R.java
在记事本中打开 R 文件并执行 "CTRL+F" 并键入资源 ID。您将找到产生错误的资源...去解决错误
快乐编码
以下是我的配置。这适用于 Android Lollipop 但在 Android Kitkat 上崩溃。
我尝试了这里的所有解决方法,但无济于事:
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>
Activity 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.access0(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.access0(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)
更新: NavigationView 的 header 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 及更高版本 can understand
中的模板 added
图像,navigation view
中的这些图像 menu items
这里是错误
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020052
......
......
android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems
如果您在 R
文件
Resource ID #0x7f020052
你会 find
它属于那个 android 没有识别(找到)它的图像,因为它可以被 android v21 识别,而不是 19 或更低
so add
png
images for pre-lollipop android devices
我有类似的问题。经过多日测试终于找到问题所在。这可能来自您的菜单项图标之一或导航 header.
已通过删除
检查导航视图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"
/>
查看我的话题并回答。希望你能解决它 Error Inflate with resourcenotfoundException
选项 1: 确保文件名匹配 - “@mipmap/icon.png”和“@mipmap/icon.xml”
选项2: 在 degub window 中,您将看到资源 ID 为:r-0X000fd something.... 转到 R 文件并检查此资源到期产生错误的地方.....
这样做: C:\Users\akshyata\Desktop\FoodSanta_Folders\FoodSanta\app\build\generated\source\r\debug\'your_package_name'\R.java
在记事本中打开 R 文件并执行 "CTRL+F" 并键入资源 ID。您将找到产生错误的资源...去解决错误
快乐编码