当前位置: 代码迷 >> 综合 >> NavigationView 加载headerlayout
  详细解决方案

NavigationView 加载headerlayout

热度:88   发布时间:2024-01-12 19:10:41.0

针对异常

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

解决

在onCreate函数内

val navigationView = findViewById<View>(R.id.nav_view_home) as NavigationView
val headerView = navigationView.getHeaderView(0)
val tvUserName = headerView.findViewById<View>(R.id.tvUserName) as TextViewtvUserName.text=(Preference.getInstance("your text")