NavigationView header image

Tags: , , , ,



I need to change NavigationView header image from fragment, but i am getting error. Someone can tell me what i am doing wrong?. Thank you for helping)))

java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.support.design.widget.NavigationView.getHeaderView(int)’ on a null object reference

My Fragment code

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        NavigationView navigationView = (NavigationView) view.findViewById(R.id.nav_view);
       //ERROR 
View headerView = (NavigationView) navigationView.getHeaderView(0);
        ImageView drawerImage = (ImageView) headerView.findViewById(R.id.header_image);
        drawerImage.setImageResource(R.drawable.header_img2);
        getActivity().setTitle("Our Projects");
        super.onViewCreated(view, savedInstanceState);
    }

navigation header xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:id="@+id/navigation_header"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/header_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:src="@drawable/header_img1" />

<ImageView
    android:id="@+id/header_logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:layout_gravity="center"
    android:scaleType="fitStart"
    app:srcCompat="@drawable/logo" />

</FrameLayout>

activity_main

<android.support.v4.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" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorWhite"
    android:clickable="false"
    app:headerLayout="@layout/nav_header_main"
    app:itemIconTint="@color/colorPrimary"
    app:itemTextColor="@color/colorPrimary"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Answer

The view you are referring here is your fragment which is not the container of the navigation drawer

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        NavigationView navigationView = (NavigationView) view.findViewById(R.id.nav_view); //this is wrong! here view is your fragment you must access the nav view using getActivity() method 
View headerView = (NavigationView) navigationView.getHeaderView(0);
        ImageView drawerImage = (ImageView) headerView.findViewById(R.id.header_image);
        drawerImage.setImageResource(R.drawable.header_img2);
        getActivity().setTitle("Our Projects");
        super.onViewCreated(view, savedInstanceState);
    }


//Try replacing with this
NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view); //if your fragment is bound to the activity containing navigation drawer


Source: stackoverflow