调用 isDrawerOpen 时导航抽屉崩溃

Navigation drawer crashes when isDrawerOpen is called

我正在尝试在 android studio 中实现导航抽屉。我发现的所有示例似乎都在使用 ListView,我更喜欢 linearLayout。

我遇到的问题是,当我调用 isDrawerOpen 时,我的应用程序崩溃 "No drawer view found with gravity LEFT"。

我在 Stack 和 Google 上搜索了一整天,但没有找到任何有用的东西。我试过:

navDrawerLayout.isDrawerOpen(Gravity.LEFT)
navDrawerLayout.isDrawerOpen(Gravity.RIGHT)
navDrawerLayout.isDrawerOpen(GravityCompat.START)
navDrawerLayout.isDrawerOpen(GravityCompat.END)
navDrawerLayout.isDrawerOpen(navLinearLayout)
android:gravity="left"
android:gravity="start"
android:gravity="left|start"
android:gravity="start|left"
android:gravity="right"
android:gravity="end"
android:gravity="right|end"
android:gravity="end|right"

以及以上的所有组合,但似乎没有任何效果。我还可能做错什么导致这次崩溃?

MainActivity.java

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;
    private DrawerLayout navDrawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private LinearLayout navLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loadToolbar();
        loadNavigationDrawer();
    }

    private void loadNavigationDrawer() {
        navDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer_layout);
        navLinearLayout = (LinearLayout) findViewById(R.id.nav_linear_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,
                navDrawerLayout,
                toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
        ) {
            public void onDrawerClosed(View view){
                super.onDrawerClosed(view);
                invalidateOptionsMenu();
                syncState();
            }
            public void onDrawerOpened(View view){
                super.onDrawerOpened(view);
                invalidateOptionsMenu();
                syncState();
            }
        };
        navDrawerLayout.setDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }

    private void loadToolbar(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                if (navDrawerLayout.isDrawerOpen(GravityCompat.START)){
                    //navDrawerLayout.closeDrawer(Gravity.LEFT);
                } else {
                    //navDrawerLayout.openDrawer(Gravity.LEFT);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
       }
    }
}

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        app:theme="@style/ToolbarTheme"
        app:popupTheme="@style/Theme.AppCompat"/>

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

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <LinearLayout
            android:id="@+id/nav_linear_layout"
            android:background="@color/white"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:gravity="start"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Header"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Item 1" />

        </LinearLayout>

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

</LinearLayout>

错误

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
        at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1293)
        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:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

要设置抽屉式导航栏,您需要 XML 以下格式:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout"
    android:background="@color/background"
    >
    <!-- This is your normal screen layout -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background">

    </LinearLayout>
    <!-- The navigation drawer layout goes here -->
    <LinearLayout
        android:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/transparent"
        android:dividerHeight="1dp"
        android:background="@color/background"
        android:orientation="vertical"
        >
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

因为我使用 layout_gravity="start" 并且支持库我也使用

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mDrawerLayout.closeDrawer(GravityCompat.START);

    mDrawerLayout.isDrawerOpen(GravityCompat.START);

试试看。

我遇到了同样的问题,在尝试之后我这样解决了:

在主 activity .xml 文件中,当我添加 NavicationBar 时,我添加了 android:layout_gravity="right" 而不是 android:layout_gravity="start"

所以结果是这样的:

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navication_menu"
    android:layout_gravity="right"/>

打开 DrawerLayout 只需写:

    DrawerLayout.openDrawer(GravityCompat.END);

希望我的回答对您有所帮助。

我是这样解决的:

if (drawerLayout.isDrawerOpen (drawerLayout.getChildAt(1)))
            drawerLayout.closeDrawer (drawerLayout.getChildAt(1));

else
            drawerLayout.openDrawer (drawerLayout.getChildAt(1));