问题
Why am i getting Null Pointer Exceptions on my NavigationView header initialized variables while setting the values from shared preferences?
Here is my code
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());
}
Log error
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)
回答1:
Try adding headerView
to navigationView
like this:
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);
回答2:
Inflate the layout and 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);
mProfileName.setText(user.getName());
mProfileEmail.setText(user.getEmail());
来源:https://stackoverflow.com/questions/37163579/null-pointer-exception-on-navigation-header-initialized-variables