使用导航膨胀布局时出错
Posted
技术标签:
【中文标题】使用导航膨胀布局时出错【英文标题】:Error on inflating layout with navigation 【发布时间】:2017-07-05 11:27:26 【问题描述】:当我用导航抽屉给我的布局充气时,充气布局时出错。这个布局有什么问题。如果有人可以,请帮助我。 我在drawer_layout之间添加了navigation_view,但它无法工作并且在膨胀布局时出错
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
app:actionViewClass="android.support.v7.widget.SearchView"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.techweblearn.musicplayer.MainActivity"
android:id="@+id/drawer_layout">
<include layout="@layout/activity_main_view"
android:layout_
android:layout_/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
activity_main_view.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
app:actionViewClass="android.support.v7.widget.SearchView"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_
android:layout_>
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/MyAppTheme"
android:layout_
android:layout_
android:gravity="center_vertical" />
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_
android:layout_
android:layout_gravity="top"
android:background="#ffffff"
app:tabIndicatorColor="@color/black" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_marginBottom="?attr/actionBarSize"
android:layout_marginTop="105dp"
android:layout_below="@id/appBar"
android:id="@+id/view_pager"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true">
</android.support.v4.view.ViewPager>
<RelativeLayout
android:elevation="16dp"
android:id="@+id/design_bottom_sheet"
android:layout_
android:layout_
android:background="#ffffff"
app:behavior_hideable="true"
app:behavior_peekHeight="50dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<fragment android:name="com.techweblearn.musicplayer.BottomPlayer"
android:id="@+id/bottom_fragment"
android:layout_
android:layout_ />
</RelativeLayout>
日志
FATAL EXCEPTION: main
Process: com.techweblearn.musicplayer, PID: 17999
java.lang.RuntimeException: Unable to start activity ComponentInfocom.techweblearn.musicplayer/com.techweblearn.musicplayer.MainActivity: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class android.support.design.widget.NavigationView
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.NavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.techweblearn.musicplayer.MainActivity.onCreate(MainActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.getColorStateList(Resources.java:992)
at android.content.Context.getColorStateList(Context.java:542)
at android.support.v7.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:65)
at android.support.design.widget.NavigationView.createDefaultColorStateList(NavigationView.java:422)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:148)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.techweblearn.musicplayer.MainActivity.onCreate(MainActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
【问题讨论】:
嗨 Kunal Puri,请在此处显示您的 gradle 文件 【参考方案1】:从DrawerLayout
和CoordinatorLayout
中删除属性app:actionViewClass="android.support.v7.widget.SearchView"
。
将menu
和headerLayout
添加到NavigationView
。
更新您的activity_main.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:layout_
android:layout_
tools:context="com.techweblearn.musicplayer.MainActivity"
android:id="@+id/drawer_layout">
<include layout="@layout/activity_main_view" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
更新您的activity_main_view.xml
如下:
<android.support.design.widget.CoordinatorLayout 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:layout_
android:layout_>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_
android:layout_>
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/MyAppTheme"
android:layout_
android:layout_
android:gravity="center_vertical" />
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_
android:layout_
android:layout_gravity="top"
android:background="#ffffff"
app:tabIndicatorColor="@color/black" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_marginBottom="?attr/actionBarSize"
android:layout_marginTop="105dp"
android:layout_below="@id/appBar"
android:id="@+id/view_pager"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true">
</android.support.v4.view.ViewPager>
<RelativeLayout
android:elevation="16dp"
android:id="@+id/design_bottom_sheet"
android:layout_
android:layout_
android:background="#ffffff"
app:behavior_hideable="true"
app:behavior_peekHeight="50dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<fragment
android:name="com.techweblearn.musicplayer.BottomPlayer"
android:id="@+id/bottom_fragment"
android:layout_
android:layout_ />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
希望对你有帮助!
【讨论】:
使用NavigationView
中添加菜单和标题布局。添加并重试...
检查行号12 >> “InflateException:二进制 XML 文件第 12 行:”。如果它的开始标签是 NavigationView,那么我认为问题出在设计支持库中。
您在 build.gralde 中使用的 Design 和 appcompat-v7 库的版本是什么?【参考方案2】:
通过添加解决了错误。
app:itemTextColor="#000"
在导航视图中
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
app:menu="@menu/nav_menu_item"
app:headerLayout="@layout/nav_header"
app:itemTextColor="#000"/>
【讨论】:
以上是关于使用导航膨胀布局时出错的主要内容,如果未能解决你的问题,请参考以下文章
Android:在drawerlayout中使用地图膨胀片段时出错
膨胀类 com.google.android.material.slider.Slider 时出错