Kotlin獲取NavigationView頭佈局中的控件報空指針

在kotlin中,你不需要findViewById()來獲取到控件,然後對其進行操作,只需要通過靜態佈局引入就可以通過對應的id進行操作了,但是並不代表你可以隨意的使用,比如說獲取NavigationView中的頭佈局裏面的控件,你需要通過NavigationView獲取HeaderView然後才能拿到他的子View了
示例代碼
主佈局代碼

<?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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

頭佈局代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_user_info"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom|center_horizontal"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/nick_name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/signature" />

</LinearLayout>

邏輯代碼

//用戶資料
nav_view.getHeaderView(0).ll_user_info.setOnClickListener {
    startActivity(Intent(this@MainActivity,UserHomeActivity::class.java))
}

至於其中getHeaderView索引爲什麼是0,我查了很多資料,說一般頭佈局基本上都是0號元素,所以填寫0

微信公衆號:Android日記


!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章