I can't setText to navigation view menu item after user is logged it. Error : NullPointerException

I can't setText to navigation view menu item after user is logged it. Error : NullPointerException

我试图在用户登录后在 NavigationView menuItem 中设置 setText 和 setDrawable。但是在用户登录后我得到了 TextView 和 CircleImageView 的 NullPointerException。这是菜单和 NavigationView

的 java 代码和 XML
    @Override
        protected void onStart() {
            super.onStart();
            userAuth.addAuthStateListener(authStateListener);
        }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        userAuth = FirebaseAuth.getInstance();

        sideMenu = findViewById(R.id.navMenu);

        authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if(firebaseAuth.getCurrentUser() != null)
                    updateActivity(true);
                else
                    updateActivity(false);
            }
        };

    private void updateActivity(Boolean isActive) {
            CircleImageView circleImageView = (CircleImageView) sideMenu.getMenu().findItem(R.id.profile_image);
            if(isActive)
            {
                Glide.with(Home.this)
                        .load(Objects.requireNonNull(userAuth.getCurrentUser()).getPhotoUrl())
                        .centerCrop()
                        .into(circleImageView);
                ((TextView)findViewById(R.id.navName)).setText(userAuth.getCurrentUser().getDisplayName());
                sideMenu.getMenu().findItem(R.id.logout).setVisible(true);
                sideMenu.getMenu().findItem(R.id.login).setVisible(false);
            }else
            {
                Glide.with(Home.this)
                        .load(R.drawable.splash_logo)
                        .centerCrop()
                        .into(circleImageView);
                ((TextView)findViewById(R.id.navName)).setText(R.string.app_name);
                sideMenu.getMenu().findItem(R.id.logout).setVisible(false);
                sideMenu.getMenu().findItem(R.id.login).setVisible(true);
            }

        }

这是菜单(home.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:id="@+id/main"
        android:checkableBehavior="single">
        <item
            android:id="@+id/login"
            android:title="Login" />
    </group>

    <group
        android:id="@+id/second"
        android:checkableBehavior="single">
        <item
            android:id="@+id/share"
            android:title="Share" />
        <item
            android:id="@+id/report"
            android:title="Report" />
    </group>

    <group
        android:id="@+id/third"
        android:checkableBehavior="single">
        <item
            android:id="@+id/license"
            android:title="Licenses"/>
        <item
            android:id="@+id/developer"
            android:title="Developer" />
        <item
            android:id="@+id/contact"
            android:title="Contact us" />

        <item
            android:id="@+id/logout"
            android:title="Logout"
            android:visible="false" />
    </group>

</menu>

这是activity.xml

中的NavigationView
<com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/home"
        android:id="@+id/navMenu"/>

navigation_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical">


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        app:civ_border_color="#ffffff"
        app:civ_border_width="2dp"
        android:src="@color/colorPrimary"/>

    <TextView
        android:id="@+id/navName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/profile_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/montserrat_bold"
        android:text="User Name"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="25sp" />

</RelativeLayout>

首先尝试从 NavigationView 获取 headerView,然后获取您的 CircleImageViewTextView,如下所示:

View headerView = sideMenu.getHeaderView(0);
CircleImageView circleImageView = (CircleImageView) headerView.findViewById(R.id.profile_image);
TextView navName = (TextView) headerView.findViewById(R.id.navName);

完整解决方案:

private void updateActivity(Boolean isActive) {

    View headerView = sideMenu.getHeaderView(0);

    CircleImageView circleImageView = headerView.findViewById(R.id.profile_image);
    TextView navName = headerView.findViewById(R.id.navName);

    if(isActive) {
        Glide.with(Home.this)
            .load(Objects.requireNonNull(userAuth.getCurrentUser()).getPhotoUrl())
            .centerCrop()
            .into(circleImageView);

        navName.setText(userAuth.getCurrentUser().getDisplayName());
        sideMenu.getMenu().findItem(R.id.logout).setVisible(true);
        sideMenu.getMenu().findItem(R.id.login).setVisible(false);
    } else {
        Glide.with(Home.this)
            .load(R.drawable.splash_logo)
            .centerCrop()
            .into(circleImageView);
        navName.setText(R.string.app_name);
        sideMenu.getMenu().findItem(R.id.logout).setVisible(false);
        sideMenu.getMenu().findItem(R.id.login).setVisible(true);
    }
}