gpt4 book ai didi

java - 导航查看标题图像

转载 作者:行者123 更新时间:2023-12-02 02:21:43 25 4
gpt4 key购买 nike

我需要从 fragment 更改 NavigationView 标题图像,但出现错误。有人可以告诉我我做错了什么吗?谢谢您的帮助)))

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

我的 fragment 代码

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

导航标题 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>

最佳答案

您在这里引用的 View 是您的 fragment ,而不是抽屉导航的容器

@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

关于java - 导航查看标题图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com