工具栏 - 重力无法找到抽屉式视图LEFT

问题描述:

我的工具栏上有3个选项卡,有一个问题。它适用于我的应用程序的所有其他页面,但由于某种原因,当我尝试点击此页面上的菜单时会崩溃。工具栏 - 重力无法找到抽屉式视图LEFT

以下是错误:

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.access$100(ActionBarDrawerToggle.java:64) 
                       at android.support.v7.app.ActionBarDrawerToggle$1.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> 

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

你不能把DrawerFragment DrawerLayout的子视图中,您必须提供抽屉布局或抽屉片段抽屉布局的子视图。解决您的问题只需将AppletLayout外部的Fragment。 就像这样:

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

你缺少有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

java.lang.IllegalArgumentException异常:没有重力发现抽屉图左侧 我想抽屉从右边打开。 子布局的宽度/高度没有为我工作,我有一个线路导致整个非法参数异常,删除行
tools:openDrawer="Right"和论据异常消失