DrawerLayout在ActionBarDrawerToggle

我正在尝试构建导航抽屉,为此我正在使用actionbardrawertoggle。我设置了参数,没有与之相关的错误。唯一的事情是我在activity_main.xml中的抽屉布局的ID无法“识别”为参数。我检查了身份证,他们都很好。我试图更改ID以查看错误中的区别,但是没有。正如您将看到的id一样。我还尝试将带工具栏的actionbardrawertoggle更改为一个,因为我认为参数顺序可能存在问题,但未进行任何更改。身份证的名称是抽屉。你知道我在想什么吗? 谢谢您的帮助

这是我的代码:

//Here is Mainactivity:

package zeinfederico.example.tasklead_newversion

import androidx.appcompat.app.AppCompatactivity
import android.os.Bundle
import android.system.Os.close
import android.system.Os.open
import androidx.appcompat.app.actionbardrawertoggle

class Mainactivity : AppCompatactivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportactionBar(findViewById(R.id.toolbar))

        val drawerToggle = actionbardrawertoggle(this,drawer,R.string.open,R.string.close)
        drawer.addDrawerListener(drawerToggle)
        drawerToggle.syncState()

        supportactionBar?.setDisplayHomeAsUpEnabled(true)
    }
} 

//Here is activity_main.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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Mainactivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:titleTextColor="@android:color/white"
            android:background="?attr/colorPrimary">
        </androidx.appcompat.widget.Toolbar>
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clipToPadding="false"
        app:menu="@menu/main_menu"
        />

</androidx.drawerlayout.widget.DrawerLayout>```

skywwh123 回答:DrawerLayout在ActionBarDrawerToggle

drawer是什么?您尚未在Java / Kotlin端初始化此类值,因此无法解析...

将其放在setContentViewsetSupportActionBar之后

val drawer = findViewById<DrawerLayout>(R.id.drawer)
本文链接:https://www.f2er.com/1250864.html

大家都在问