Android kotlin-在导航菜单内设置文本视图文本错误:视图不能为空

Android kotlin-在导航菜单内设置文本视图文本错误:视图不能为空,android,kotlin,Android,Kotlin,我使用的导航抽屉使用nav_header.xml(内部菜单文件夹)作为顶部 我正在尝试更改其中的文本视图文本,代码如下: toolbar = findViewById(R.id.toolbar) setSupportActionBar(toolbar) drawerLayout = findViewById(R.id.drawer_layout) navView = findViewById(R.id.nav_view) val toggle = ActionBarDrawerToggle(

我使用的导航抽屉使用nav_header.xml(内部菜单文件夹)作为顶部

我正在尝试更改其中的文本视图文本,代码如下:

toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)

val toggle = ActionBarDrawerToggle(
    this, drawerLayout, toolbar, 0, 0
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)

val nickname = "testttt"

MenuNickname.text = nickname
错误是:

MenuNickname must not be null
我该怎么做?

这是解决方案:

val nickname = "testttt"

var headerLayout = navView.getHeaderView(0)

headerLayout.MenuNickname.text = nickname
根据这一回答: