Android Can';不要将相对位置添加到抽屉布局

Android Can';不要将相对位置添加到抽屉布局,android,android-layout,drawerlayout,Android,Android Layout,Drawerlayout,我的主要活动 protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); xml是: <and

我的主要活动

protected void onCreate(Bundle savedInstanceState) 
{ 
    setContentView(R.layout.activity_main);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
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"
    android:background="@android:color/black" >

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
          android:id="@+id/sample_content_fragment"
          android:background="#242a31"
          android:layout_weight="2"
          android:layout_width="match_parent"
          android:layout_height="0px" >


    </FrameLayout>

    <!-- 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.


          -->

    <RelativeLayout
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/left_LinearLayout"
            android:orientation="vertical"
            android:layout_gravity="start"
            android:layout_width="240dp"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText" />


        </LinearLayout>
        <ListView
            android:below="@+id/left_LinearLayout"
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:listSelector="@drawable/selector_bg_key"
            android:background="@color/default_color"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
        </RelativeLayout>


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

但应用程序因错误而崩溃:

android.widget.LinearLayout$LayoutParams不能强制转换为
android.support.v4.widget.DrawerLayout$LayoutParams


我需要将编辑字段添加到左侧菜单,或者我应该通过适配器添加它,您可能正在尝试以下操作:

mDrawerLayout.openDrawer(mDrawerList);
但是,您的
列表视图
的父项不是
抽屉布局
。现在,您应该使用
RelativeLayout
作为您的抽屉:

<RelativeLayout
    android:id="@+id/left_drawer"
    android:layout_gravity="start"
    android:layout_width="240dp"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/left_LinearLayout"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:layout_width="240dp"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText" />
    </LinearLayout>
    <ListView
        android:layout_below="@+id/left_LinearLayout"
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</RelativeLayout>
然后像这样打开抽屉:

mDrawerLayout.openDrawer(mDrawer);

看起来您需要添加更多代码。
mDrawerLayout.openDrawer(mDrawer);