0

我在 XML 代码中有这样的 NavigationView:

        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" />

在 Java 代码中,我使用 ButterKnife 来加载它。

    @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);
}

这是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>

这是 MainHeaderViewHolder 类:

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);
  }
}

现在我调用演示者在该标题中加载有关用户的一些信息。

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

当它尝试设置用户名和电子邮件时,它会在这些行上崩溃。

为什么?

 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)
4

1 回答 1

0

当我从 MainHeaderViewHolder 中删除 ButterKnife 时,它​​开始正常工作。

于 2020-03-11T08:01:25.347 回答