2014-02-18 49 views
1

抽屉布局使用了两个相对布局,引发错误为:的Android导航抽屉布局引发错误

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> 

9月2日至18日: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); 
     } 

它throwa误差作为 “的InvocationTargetException” 行是

布尔drawerOpen = mDrawerlayout

+0

其进口错误,请使用正确的进口。 –

+0

现在一次检查是否也粘贴了xml代码,请问是什么样的正确导入 –

+0

看到,你试着把'RelativeLayout'的布局参数设置为'DrawerLayout'在你的代码的某个地方这样纠正这个 –

回答

0

尝试代替

boolean drawerOpen = mDrawerLayout.isDrawerVisible(Gravity.START);

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

此外,我没有看到你的XML中的任何NavigationDrawer。确保你有一个。

你应该有<android.support.v4.widget.DrawerLayout />为你的父标签

UPDATE

现在你没有显示不drawerlayout的观点,你应该写一个FrameLayout持有无抽屉要显示的正常项目。

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); 

这不是崩溃。