Search code examples
androidnavigationview

SetText on navigation view header


I'm working with firebase auth authentication and with the navigation drawer layout, what I need is to set the user email on the header of my navigation, the thing is as I got, is that it can't find the related textView on my layout, because the main activity is associated with my main layout and not the navigation itself, I did a test to see if the user retrieved from the database, and I got the email, but when I do this:

auth = FirebaseAuth.getInstance();
String email = auth.getCurrentUser().getEmail();
Log.d("email",email);

if (auth.getCurrentUser() != null) {
    userTxt.setText(email);
}

I get a NullPointerException, that I think is related to the userTxt, so if anyone experienced it before can you guys give me a tip about how to solve this?

StackTrace

FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 31473
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esmad.pdm.friendlymanager/com.esmad.pdm.friendlymanager.MainActivity}: 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:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     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.esmad.pdm.friendlymanager.MainActivity.onCreate(MainActivity.java:42)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372)

Solution

  • From the Exception it is clear that your userTxt is null.

    To access NavigationView Header -- you need to first get the Header view:

    From Documentation

    View getHeaderView (int index) Gets the header view at the specified position.

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    View header = navigationView.getHeaderView(0) ;
    TextView userTxt = (TextView) header.findViewById(R.id.textView); //your ID
    userTxt.setText(email);