没有发现重力左侧的抽屉视图

No drawer view found with gravity LEFT

当我 运行 应用程序并单击工具栏图标时,我收到错误消息“未找到重力左侧的抽屉视图”

这是我的 xml 文件

main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/book_list_rv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

        <ListView
            android:id="@+id/ListView1"
            android:layout_width="241dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#333"
            android:choiceMode="singleChoice"
            android:divider="#666"
            android:dividerHeight="1dp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp" />
    </LinearLayout>

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

此 ID activity java 文件的代码片段 activity 文件

Toolbar mToolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // drawer
        setSupportActionBar(mToolbar);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                mToolbar, R.string.app_name, R.string.app_name);
        mDrawerLayout.setDrawerListener(mDrawerToggle);

内容和抽屉应该在不同的布局中,抽屉应该有重力。

DrawerLayout

Contnet

Drawer

/DrawerLayout

像这样更新你的文件,

<LinearLayout ....
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/book_list_rv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
        </LinearLayout>

        <ListView
            android:id="@+id/ListView1"
            android:layout_width="241dp"
            android:layout_height="match_parent"
            android:layout_gravity="left" //or start
            android:background="#333"
            android:choiceMode="singleChoice"
            android:divider="#666"
            android:dividerHeight="1dp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp" />
    </LinearLayout>

Content 和 Drawer 必须是 DrawerLayout 的两个子级。所以像这样改变你的布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/book_list_rv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

    </LinearLayout>

    <ListView
        android:id="@+id/ListView1"
        android:layout_width="241dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#333"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:paddingLeft="15sp"
        android:paddingRight="15sp" />

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

先设置drawerlayout再设置support action bar。 检查以下代码。

Toolbar mToolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                mToolbar, R.string.app_name, R.string.app_name);
 mDrawerLayout.setDrawerListener(mDrawerToggle);
 setSupportActionBar(mToolbar);//modified(changed line)

可能与您尝试关闭抽屉的方式有关

mToolBar.setNavigationOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mDrawerLayout.isDrawerOpen(Gravity.Start)) {
            mDrawerLayout.closeDrawer(Gravity.Start);
        } else {
            mDrawerLayout.openDrawer(Gravity.Start);
        }
    }

更多参考

https://developer.android.com/reference/android/support/v4/view/GravityCompat.html#START