抽屉布局使用两个相对布局,抛出错误:
xml文件:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Listview to display slider menu -->
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" >
<ExpandableListView
android:id="@+id/list_slidermenu"
android:layout_width="197.50dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="#2f2f2f"
android:choiceMode="singleChoice"
android:divider="@drawable/divider"
android:dividerHeight="0.5dp"
android:groupIndicator="@android:color/transparent"
android:listSelector="#2FB3E3" />
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:id="@+id/build"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingRight="100dp"
android:text="My View"
android:textColor="#FFFFFF" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
02-18 09:22:53.649:E / AndroidRuntime(30768): java.lang.ClassCastException: android.widget.RelativeLayout $ LayoutParams无法强制转换为 android.support.v4.widget.DrawerLayout $的LayoutParams
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
if () {
--------
} else {
--------
}
return super.onPrepareOptionsMenu(menu);
}
抛出错误,因为“InvocationTargetException”行是
boolean drawerOpen = mDrawerlayout
答案 0 :(得分:0)
尝试
boolean drawerOpen = mDrawerLayout.isDrawerVisible(Gravity.START);
而不是
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
另外,我在你的xml中看不到任何NavigationDrawer
。确保你有一个。
您应该将<android.support.v4.widget.DrawerLayout />
作为父标记
<强>更新强>
现在你没有在没有drawerlayout的情况下显示的视图,你应该写一个FrameLayout
来保存没有抽屉的正常项目。
答案 1 :(得分:0)
我的代码中出现了同样的问题。这是我的代码:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".HomeActivity"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/toolbar"
android:padding="4dp"
android:clipToPadding="false"
android:columnWidth="@dimen/item_width"
android:numColumns="auto_fit"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:stretchMode="columnWidth" />
</RelativeLayout>
<!-- Left drawer -->
<RelativeLayout
android:id="@+id/theDrawerRelativeLayout"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/windowBackgroundColor"
android:orientation="vertical" >
<ListView
android:id="@+id/theDrawer"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:divider="#FFF"
/>
<ImageView
android:id="@+id/drawerLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/theDrawer"
android:clickable="false"
android:contentDescription="imagen"
android:fitsSystemWindows="true"
android:longClickable="false" />
</RelativeLayout>
<!-- Left drawer -->
<!-- Right drawer -->
<ListView
android:id="@+id/theDrawerRight"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/windowBackgroundColor"/>
<!-- Right drawer -->
当我执行该行时:
mDrawer.closeDrawer(mDrawerList);
该应用程序崩溃了。那么诀窍是什么?传递给closeDrawer的参数包含你的最长的RelativeLayout,其中包含抽屉中显示的List(抽屉本身)。所以代码仍然是这样的:
mDrawer.closeDrawer(mDrawerRelativeLayout);
那不会崩溃。