【发布时间】:2018-02-16 19:57:04
【问题描述】:
我想包含一个包含 2-3 个固定项目的菜单,这些项目永远不会改变,我想在菜单下方包含一个我可以动态管理的 ListView。
我想做类似下图的事情。
当我启动应用程序时,我总是收到以下错误:
Process: com.pigmalionstudios.todolist, PID: 7055
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class menu
MainActivity.java中对应的错误行是:
setContentView(R.layout.activity_main);
这是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: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_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_header_main" />
<include layout="@menu/drawer_menu"/>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:textColor="#424242"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
还有drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
android:id="@+id/activity_main_drawer"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:orientation="vertical">
<group android:checkableBehavior="single">
<item
android:id="@+id/add"
android:icon="@drawable/add"
android:title="@string/add" />
<item
android:id="@+id/ToDo"
android:icon="@drawable/pending"
android:title="To Dos" />
<item
android:id="@+id/settings"
android:icon="@drawable/settings"
android:title="Settings" />
</group>
</menu>
【问题讨论】:
-
你关闭菜单了吗??
-
不,这发生在启动时。我没有调用 closeDrawer()
-
你是否关闭了 xml 中的菜单标签??
-
抱歉,帖子中的格式不正确。是的,菜单的结束标签在那里。
-
做一件事注释掉listview部分并检查菜单是否有效..如果它不起作用那么我认为菜单背景可绘制导致问题给它一些其他背景..
标签: java android xml navigation-drawer