【问题标题】:Android Navigation Drawer Layout throws an errorAndroid导航抽屉布局抛出错误
【发布时间】:2014-11-07 19:20:08
【问题描述】:

抽屉布局使用两个Relative Layout,报错为:

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”行是

布尔抽屉打开=mDrawerlayout

【问题讨论】:

  • 导入错误,请使用正确的导入。
  • 现在检查是否也粘贴了 xml 代码,什么样的正确导入
  • 看,您尝试将 RelativeLayout 的布局参数放到 DrawerLayout 代码中的某个位置,因此请更正此
  • 在 DrawerLayout 中,我希望 textview 在底部
  • 但是你的DrawerLayout 在哪里

标签: android android-layout android-intent android-fragments


【解决方案1】:

试试

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

而不是

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

另外,我在您的 xml 中没有看到任何 NavigationDrawer。确保你有一个。

你应该有&lt;android.support.v4.widget.DrawerLayout /&gt;作为你的父标签

更新

现在你没有在没有抽屉布局的情况下显示视图,你应该写一个 FrameLayout 来保存要在没有抽屉的情况下显示的正常项目。

【讨论】:

    【解决方案2】:

    我的代码中也出现了同样的问题。这是我的代码:

    <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,其中包含抽屉中显示的列表(抽屉本身)。让代码保持这种状态:

    mDrawer.closeDrawer(mDrawerRelativeLayout);
    

    这不会崩溃。

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多