抽屉菜单汉堡包按钮在创建时不起作用

Drawer menu hamburger button doesn`t work on create

所以,问题是我的汉堡包按钮在应用程序启动时不起作用 - 它会切换,但抽屉菜单不会打开。当我从侧面滑动打开和关闭抽屉菜单时,它开始工作......因此每次启动任何新的 activity 时。

这是我的科特林代码:

open class BaseActivity : AppCompatActivity(), Observer, NavigationView.OnNavigationItemSelectedListener {

private var drawer: DrawerLayout? = null

fun oncreate() {


    val toolbar: android.support.v7.widget.Toolbar = this.findViewById(R.id.toolbar)

    setSupportActionBar(toolbar)
    val actionBar = this.supportActionBar
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true)
        actionBar.setHomeButtonEnabled(true)
    }
    //val actionbar: ActionBar? = supportActionBar
    drawer = findViewById(R.id.drawer_layout)
    val navigationView: NavigationView = findViewById(R.id.nav_view)
    navigationView.setNavigationItemSelectedListener(this)

    toggle = ActionBarDrawerToggle(
        this, drawer, toolbar,
        R.string.navigation_drawer_open, R.string.navigation_drawer_close
    )
    actionBar?.apply {
        setDisplayHomeAsUpEnabled(true)
        setHomeAsUpIndicator(R.drawable.ic_menu)
    }

    drawer!!.addDrawerListener(toggle)
    toggle.syncState()

}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {

        android.R.id.home -> {
            drawer!!.openDrawer(GravityCompat.START)
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        android.R.id.home -> {
            drawer!!.openDrawer(GravityCompat.START)
            true
        }
        //other items
    }
    drawer!!.closeDrawer(GravityCompat.START)
    return true
}

和 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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@drawable/background"
        tools:openDrawer="start" android:visibility="visible">

    <LinearLayout
            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/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:visibility="visible"/>

//这里有更多元素

    </LinearLayout>

    <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:itemTextAppearance="@style/txt_expandable"
            app:menu="@menu/drawer_menu" android:visibility="gone">
    </android.support.design.widget.NavigationView>

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

我已经尝试了很多方法来解决这个问题,但似乎没有任何效果...我完全按照 Kotlin 文档中的步骤进行操作,但问题仍然存在。

这是错误:

app:menu="@menu/drawer_menu" android:visibility="gone">

删除android:visibility="gone"。应该是:

app:menu="@menu/drawer_menu">