【问题标题】:Relative layout inside drawer layout: relative layout params cannot be cast into drawer layout params抽屉布局内的相对布局:不能将相对布局参数转换为抽屉布局参数
【发布时间】:2014-08-17 05:06:27
【问题描述】:

我正在尝试在抽屉内使用相对布局,以便可以包含列表视图和网格视图。运行时收到以下错误:

Logcat 错误:

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

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">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <!-- ListView to display slider menu -->
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="240dp"
        android:layout_height="fill_parent"
         android:layout_gravity="start"
        android:id="@+id/drawer_relative_layout"
        >
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"      
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"         
     />
       <GridView
        android:id="@+id/images_drawer"
        android:layout_width="240dp"
        android:layout_height="fill_parent"
        android:layout_below="@id/left_drawer"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"      
        android:background="@color/list_background"
        android:layout_margin="5dp"
        android:columnWidth="100dp"
        android:drawSelectorOnTop="true"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
         />
   </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 发布完整的错误堆栈跟踪
  • 嘿,你能解决这个问题吗??

标签: android android-layout layout android-relativelayout


【解决方案1】:

请检查您的 java 代码。确保您使用正确的布局参数。您可能指的是抽屉布局以外的布局。

可能发生在这里:

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

或者这里:

drawerLayout.closeDrawer(drawerLayout);

【讨论】:

    【解决方案2】:

    我刚刚完成 POC 运行试试看, 这是布局

    <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" >
    
    <!--
         As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions.
    -->
    
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <!--
         android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view.
    -->
    
    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:orientation="vertical" 
            android:background="@android:color/holo_orange_light">
    
            <ImageView
                android:id="@+id/ivwLogo"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/medicinecarticon" />
    
            <CheckBox
                android:id="@+id/jji"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_toRightOf="@+id/ivwLogo"
                android:text="huhuuhul" />
        </RelativeLayout>
    
        <ListView
            android:id="@+id/left_drawer_child"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>
    

    对于主要活动,请下载完整的代码库 https://drive.google.com/file/d/0B_1gIYJI3B4FWDE0bGpMZnhGQmc/view?usp=sharing 上传完整的项目希望对您有所帮助

    【讨论】:

      【解决方案3】:

      用途:

      DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      

      你正在使用:

      DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_relative_layout);
      

      DrawerLayout 总是从 android.support.v4.widget.DrawerLayout 视图而不是从您包含在抽屉中的布局中膨胀。

      【讨论】:

      • 我在主要活动中使用了drawer_layout。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2015-11-02
      相关资源
      最近更新 更多