ともちゃんのアプリ開発日記

組込みC言語プログラマだったともちゃんが、四苦八苦しながら、AndroidのJAVA/Kotlin、iOSのSwiftUIを習得して行きます。ともちゃんの備忘録も兼ねています。

Drawer内のNavigation Header内にSpinnerを置く

Drawer内のNavigation Header内にSpinnerを置き、正常に動作させるのに苦労しました。

やりたいこと

下記の様に、Drawer内のNavigation Header内にSpinnerを置きたい。

f:id:uchida001tmhr:20210221061844j:plain

DrawerのSpinner

Layout XML

activity_min.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_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>

 

 nav_header_main.xml

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

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/ic_health_log_48x48"
android:contentDescription="@string/app_name"/>

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

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinnerSelectUser"
android:layout_marginTop="20dp" />

</LinearLayout>

 

 問題となったソースコード

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);

.......

Spinner spinner = drawer.findViewById(R.id.spinnerSelectUser);

.......

}
});

 Spinner spinner = drawer.findViewById(R.id.spinnerSelectUser);

でNull Pointer Exceptionで落ちることがありました。

うまくいったソースコード

これを、

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);

......

NavigationView navView = drawer.findViewById(R.id.nav_view);
View parentView = navView.getHeaderView(0);
Spinner spinner = parentView.findViewById(R.id.spinnerSelectUser);

......

}
});

のように直したら、クラッシュしなくなりました。