androidandroid-navigationviewandroid-drawer

NavigationView Header view items null


I have NavigationView like this in XML code:

        android:id="@+id/activityMainNavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"
        app:itemIconTint="@color/colorPrimary"
        app:itemTextColor="@color/colorPrimary"
        app:itemTextAppearance="@style/Base.TextAppearance.AppCompat.Menu"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" />

While in Java code I use ButterKnife to load it.

    @BindView(R.id.activityMainNavigationView) NavigationView navigationView;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    injector().inject(this);
    menu = navigationView.getMenu();
    View headerView  = navigationView.getHeaderView(0);
    headerViewHolder = new MainHeaderViewHolder(headerView);
}

Here is nav_header

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginTop="@dimen/default_screen_margin_very_big"
    android:paddingLeft="@dimen/default_screen_margin_normal"
    android:paddingRight="@dimen/default_screen_margin_normal"
    android:paddingTop="32dp"
    >

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/activityMainIvPhoto"
        android:layout_width="@dimen/drawer_user_image"
        android:layout_height="@dimen/drawer_user_image"
        android:src="@drawable/ic_profile_placeholder"
        app:civ_border_color="@color/colorPrimary"
        app:civ_border_width="2dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/activityMainIvPhoto"
        android:paddingStart="@dimen/default_screen_margin_normal"
        android:orientation="vertical">

    <TextView
        android:id="@+id/activityMainTvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="User name"
        android:textColor="@color/colorPrimary" />

    <TextView
        android:id="@+id/activityMainTvEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/default_screen_margin_very_small"
        tools:text="email@email.com"
        android:textSize="12sp"
        android:textColor="@color/colorPrimary" />

    </LinearLayout>
</RelativeLayout>

Here it is MainHeaderViewHolder class:

public class MainHeaderViewHolder {

  @BindView(R.id.activityMainIvPhoto) CircleImageView ivPhoto;
  @BindView(R.id.activityMainTvName) TextView tvName;
  @BindView(R.id.activityMainTvEmail) TextView tvEmail;

  public MainHeaderViewHolder(View view) {
    ButterKnife.bind(this , view);
  }
}

Now I call presenter to load some info about user in that header.

  @Override public void displayUser(User user) {
    if(headerViewHolder != null) {
      showUserImageIfWeHaveIt(user);
        headerViewHolder.tvName.setText(user.getName());
        headerViewHolder.tvEmail.setText(user.getEmail());
      }
    }
  }

And it crashes on those lines when it tries to set user name and email.

Why?

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.test.main.MainActivity.displayUser(MainActivity.java:107)
        at com.test.presentation.main.MainPresenter.lambda$getCurrentUser$0$MainPresenter(MainPresenter.java:47)
        at com.test.presentation.main.-$$Lambda$MainPresenter$CY2qoRWfEu3xRMCmrAS2UZENsMg.accept(Unknown Source:4)
        at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:62)
        at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
        at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:124)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Solution

  • When I removed ButterKnife from MainHeaderViewHolder it started working properly.