工具栏 - 左重力未找到抽屉视图

Toolbar - No drawer view found with gravity LEFT

我的工具栏在一个 activity 上有 3 个选项卡的问题。它适用于我应用程序的所有其他页面,但由于某种原因,当我尝试单击此页面上的菜单时崩溃。

这里是错误:

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
                                                                                at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1618)
                                                                                at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:290)
                                                                                at android.support.v7.app.ActionBarDrawerToggle.access0(ActionBarDrawerToggle.java:64)
                                                                                at android.support.v7.app.ActionBarDrawerToggle.onClick(ActionBarDrawerToggle.java:200)
                                                                                at android.view.View.performClick(View.java:5697)
                                                                                at android.view.View$PerformClick.run(View.java:22526)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这里是布局的xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:id="@+id/main_content"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent"
                                             android:fitsSystemWindows="true"
                                             >


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="0dp">

    <include android:id="@+id/app_bar"
             layout="@layout/toolbar" />


    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="48dp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_below="@id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



        <fragment
            android:name="com.bestworkouts.sheikoworkout.NavigationDrawerFragment"
            android:id="@+id/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer" />




</android.support.design.widget.AppBarLayout>

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

谢谢,希望有人能帮帮我!

您缺少 CoordinatorLayout 最后的关闭标记:</android.support.design.widget.CoordinatorLayout>

但是,请注意 DrawerLayout 应该是顶级布局。

来自文档:

To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

所以我认为您不应该将它 (DrawerLayout) 放在 CoordinatorLayout 中。

Android DrawerLayout - No drawer view found with gravity

您不能将 DrawerFragment 放入 DrawerLayout 的子视图中您必须提供抽屉布局或抽屉片段作为抽屉布局的子视图。解决您的问题只需将 Fragment 放在 AppbarLayout 之外。 就像那样:

        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:id="@+id/main_content"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:fitsSystemWindows="true"
                                                 >


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="0dp">

        <include android:id="@+id/app_bar"
                 layout="@layout/toolbar" />


        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="48dp"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_below="@id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>







    </android.support.design.widget.AppBarLayout>

            <fragment
                android:name="com.bestworkouts.sheikoworkout.NavigationDrawerFragment"
                android:id="@+id/fragment_navigation_drawer"
                tools:layout="@layout/fragment_navigation_drawer"
                android:layout_width="280dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:layout="@layout/fragment_navigation_drawer" />


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

java.lang.IllegalArgumentException: 未找到重力为 LEFT 的抽屉视图 我想让抽屉从右边缘打开。 子布局宽度/高度对我不起作用,我有一行导致整个非法参数异常,删除了行
tools:openDrawer="Right" 参数异常消失了