Android DrawerLayout - No drawer view found with gravity

前端 未结 9 543
执笔经年
执笔经年 2020-11-27 06:23

When I click on my drawer toggle I get the following exception:

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

9条回答
  •  爱一瞬间的悲伤
    2020-11-27 07:10


    Ok. Let me make the things simple and clear here.
    What is DrawerLayout?https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

    DrawerLayout Acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from the edge of the window. Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.) 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.


    What is DrawerLayout?, In Simple words:
    • The layout supported by Android that allows you to have an overlay screen, called as drawer, over your main screen.
    • DrawerLayout needs childviews to work, similar to the LinearLayout and RelativeLayout.
    • The childview must have either of the property set
      android:layout_gravity="start" OR android:layout_gravity="left"
    • DrawerLayout uses this childView to know from where to start showing the Drawer i.e. From Left to Right or Right to Left. There are countries where the text is read from Right to Left.


    Techie Stuff:
    • EVENT#1: ActionBarDrawerToggle.java is set as the drawer listener for the DrawerLayout
    •     DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
          ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                      toolbar,R.string.navigation_drawer_open,
                      R.string.navigation_drawer_close)
                      mDrawerLayout.setDrawerListener(mDrawerToggle);
          
    • EVENT#2: The user clicks on the Toolbar.navigationIcon that calls the toggle() function
      ActionBarDrawerToggle.java (android-sdk\sources\android-22\android\support\v7\app\ActionBarDrawerToggle.java)
          private void toggle() { 
              if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
                  mDrawerLayout.closeDrawer(GravityCompat.START); 
              } else {
                  mDrawerLayout.openDrawer(GravityCompat.START); 
              } 
          } 
          

      Gravity.java (android-sdk\sources\android-22\android\view\Gravity.java)
      /** Push object to x-axis position at the start of its container,
          not changing its size.*/ 
          public static final int START = RELATIVE_LAYOUT_DIRECTION | Gravity.LEFT; 
          
    • EVENT#3: Toggle() function calls the mDrawerLayout.openDrawer(Gravity.START)
      DrawerLayout.java (support-v4-22.1.1.jar library)
          /*** Open the specified drawer by animating it out of view. **
          @param gravity Gravity.LEFT to move the left drawer or Gravity.RIGHT
          for the right. * GravityCompat.START or GravityCompat.END may also
          be used. */ 
          public void openDrawer(@EdgeGravity int gravity) { 
              final View drawerView = findDrawerWithGravity(gravity); 
              if (drawerView == null) { 
                  throw new IllegalArgumentException("No drawer view found with gravity " +
          gravityToString(gravity)); 
              }
              openDrawer(drawerView); 
          }
          
    • EVENT#4: If no childView of the DrawerLayout is set with the layout_gravity="start” or left the No drawer view found with gravity LEFT is thrown.

    Solution:

        
         
         
             
         
        
        
         
    
        
    I hope that helps. Happy Coding…
    

提交回复
热议问题