Исключение нулевого указателя в инициализированных переменных заголовка навигации

Почему при инициализации значений из общих настроек я получаю исключения для пустых указателей в инициализированных переменных заголовка NavigationView?

Вот мой код

 private void setNavigationHeader() {
    View navHeader = mNavigationView.getHeaderView(0);

    TextView mProfileName = (TextView) navHeader.findViewById(R.id.profile_name);
    TextView mProfileEmail = (TextView) navHeader.findViewById(R.id.profile_email);

    User user = Utils.getCurrentUser(MainActivity.this);

119 mProfileName.setText(user.getName());
    mProfileEmail.setText(user.getEmail());
}

Ошибка журнала

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.userapp/com.myapp.userapp.MainActivity}: java.lang.NullPointerException
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                  Caused by: java.lang.NullPointerException
                                                                     at com.myapp.userapp.MainActivity.setNavigationHeader(MainActivity.java:119)
                                                                     at com.myapp.userapp.MainActivity.onCreate(MainActivity.java:97)
                                                                     at android.app.Activity.performCreate(Activity.java:5231)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                     at dalvik.system.NativeStart.main(Native Method) 

2 ответа

Раздувать макет и навигациюПросмотреть

LinearLayout navHeader=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(linearLayout);

TextView mProfileName = (TextView) navHeader.findViewById(R.id.profile_name);
TextView mProfileEmail = (TextView) navHeader.findViewById(R.id.profile_email);
mProfileName.setText(user.getName());
mProfileEmail.setText(user.getEmail());

Попробуйте добавить headerView в navigationView как это:

LinearLayout navHeader=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(linearLayout);

TextView mProfileName = (TextView) navHeader.findViewById(R.id.profile_name);
TextView mProfileEmail = (TextView) navHeader.findViewById(R.id.profile_email);