总是收到错误消息:没有找到重力左侧的抽屉视图

Always getting the error messsage: No drawer view found with gravity LEFT

我正在开发一个应用程序,但我在抽屉布局方面遇到了一些问题。我总是收到标题中提到的错误消息,但我没有 LEFT 值的重力。我所拥有的只是具有 END 值的重力,仅此而已。 这是我的 Java 代码:

public class HomeActivity extends AppCompatActivity {

private ActionBarDrawerToggle toggle;

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

    Toolbar mainToolBar =findViewById(R.id.main_page_toolbar);
    DrawerLayout mainDrawerLayout = findViewById(R.id.main_page_drawer_layout);
    NavigationView mainNavigationView = findViewById(R.id.main_page_navigation_view);

    setSupportActionBar(mainToolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);


    toggle = new ActionBarDrawerToggle(this,mainDrawerLayout,mainToolBar,R.string.openDrawer,R.string.closeDrawer);
    mainDrawerLayout.addDrawerListener(toggle);
    toggle.syncState();

}

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

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
 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:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:openDrawer="end"
 tools:context=".Activities.HomeActivity"
 android:id="@+id/main_page_drawer_layout"
 android:fitsSystemWindows="true">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/home_main_page"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    app:headerLayout="@layout/home_side_menu_header_layout"
    app:menu="@menu/home_side_menu"
    android:layoutDirection="rtl"
    android:id="@+id/main_page_navigation_view"/>

您需要处理 navigation 单击 Toolbar,如下所示:

mainToolBar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mainDrawerLayout.isDrawerOpen(GravityCompat.END))
            mainDrawerLayout.closeDrawer(GravityCompat.END);
        else
            mainDrawerLayout.openDrawer(GravityCompat.END);
    }
});

也不要忘记在需要时关闭抽屉,如下所示:

mainDrawerLayout.closeDrawer(GravityCompat.END)