NavigationView标题图片

时间:2018-01-23 12:40:35

标签: java android xml android-fragments navigationview

我需要从片段更改NavigationView头图像,但我收到错误。有人可以告诉我我做错了什么吗?谢谢你的帮助)))

  

java.lang.NullPointerException:尝试调用虚方法   “android.view.View   android.support.design.widget.NavigationView.getHeaderView(int)'上的一个   null对象引用

我的片段代码

    @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>

3 个答案:

答案 0 :(得分:1)

您在此处引用的视图是您的片段,它不是导航抽屉的容器

@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

答案 1 :(得分:0)

使用像这样的id;

NavigationView navigationView = (NavigationView)   view.findViewById(R.id.nav_view);

View headerView = navigationView.findViewById(R.id.navigation_header);
    ImageView drawerImage = (ImageView) headerView.findViewById(R.id.header_image);
    drawerImage.setImageResource(R.drawable.header_img2);
    getActivity().setTitle("Our Projects");

答案 2 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header_relativelayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/header_color"
android:gravity="center"
android:paddingLeft="@dimen/large_x_padding">

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/profile_pic" />

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/iv_image"
    android:layout_alignTop="@+id/iv_image"
    android:layout_centerVertical="true"
    android:layout_marginLeft="@dimen/layout_margin_left"
    android:layout_toLeftOf="@+id/hreader_direction"
    android:layout_toRightOf="@+id/iv_image"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_mobile_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="8793019240"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_18sp" />


    <TextView
        android:id="@+id/txt_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Ways means"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_12sp" />

</LinearLayout>

<ImageView
    android:id="@+id/hreader_direction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:background="?selectableItemBackgroundBorderless"
    android:clickable="true"
    android:src="@drawable/header_deriection" />