1

I want to change the drop down image and text programmatically when you create a new Navigation Drawer Activity. I tried this: (Assume currentUser.firstName is not null.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    TextView message  = (TextView) navigationView.findViewById(R.id.welcomeTextview);
    message.setText("Hello " + currentUser.firstName.toString());

    ImageView profilePicbtn = (ImageView)navigationView.findViewById(R.id.imageViewProfile);
    profilePicbtn.setBackgroundResource(R.drawable.nat_profile);

I did 'navigationView.findviewById()' because the imageView and TextView are in nav_header_dashboard.xml. When I run this, it crashes, if I remove the TextView and ImageView declaration it runs. I just want to change the picture at the top and provide a hello message when I click on the drop down menu. Also assume the image 'nat_profile' exits.

These are my files:

Dashboard.java

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.*;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

public class Dashboard extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //testing lazaro
    //TextView message  = (TextView) navigationView.findViewById(R.id.welcomeTextview);
    //message.setText("Hello " + currentUser.firstName.toString());

    ImageView profilePicbtn = (ImageView)navigationView.findViewById(R.id.imageViewProfile);
    profilePicbtn.setBackgroundResource(R.drawable.nat_profile);

activity_dashboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<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_dashboard"
    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:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_dashboard"
    app:menu="@menu/activity_dashboard_drawer" />

</android.support.v4.widget.DrawerLayout>

nav_header_dashboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageViewProfile"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@drawable/nat_profile" />

<TextView
    android:id="@+id/welcomeTextview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

</LinearLayout>

This is my strings.xml in values folder:

<resources>
   <string name="app_name">Life Management</string>
   <string name="title_activity_login">Sign in</string>

   <!-- Strings related to login -->
   <string name="prompt_email">Email</string>
   <string name="prompt_password">Password (optional)</string>
   <string name="action_sign_in">Sign in or register</string>
   <string name="action_sign_in_short">Sign in</string>
   <string name="error_invalid_email">This email address is invalid</string>
   <string name="error_invalid_password">This password is too short</string>
   <string name="error_incorrect_password">This password is incorrect</string>
   <string name="error_field_required">This field is required</string>
   <string name="permission_rationale">"Contacts permissions are needed for providing email
    completions."
   </string>
   <string name="title_activity_dashboard">DashboardActivity</string>

<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>

<string name="action_settings">Settings</string>


<string name="action_signout">Sign Out</string>
<string name="action_joy">JOY</string>
<string name="action_passion">PASSION</string>
<string name="action_givingback">GIVING BACK</string>
<string name="section_format">Hello World from section: %1$d</string>
<string name="title_activity_previous_cycle">Life Management</string>
<string name="title_home">Home</string>
<string name="title_dashboard">Dashboard</string>
<string name="title_notifications">Notifications</string>
</resources>

Crash Report:

11-05 10:37:32.621 2890-2890/com.example.seniorprojectfall.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.seniorprojectfall.test, PID: 2890
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.seniorprojectfall.test/com.example.seniorprojectfall.test.Dashboard}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at **com.example.seniorprojectfall.test.Dashboard.onCreate(Dashboard.java:354)**
                                                                                  at android.app.Activity.performCreate(Activity.java:6975)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                  at android.os.Looper.loop(Looper.java:164) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Dashboard.java:354) is the line: message.setText("Hello " + currentUser.firstName.toString());

Lazaro
  • 29
  • 1
  • 2
  • 6

2 Answers2

3

Change

TextView message = (TextView) navigationView.findViewById(R.id.welcomeTextview);

To( I've added getHeaderView )

TextView message = (TextView) navigationView.getHeaderView(0).findViewById(R.id.welcomeTextview);

Also do this for second textVew

Valentun
  • 1,661
  • 12
  • 23
1

You can try finding your Navigation drawer header layout like below

View headerLayout = navigationView.getHeaderView(0);

instead of

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

and then you can find your textview like this

TextView message  = (TextView) navigationView.findViewById(R.id.welcomeTextview);
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84