为什么在设置共享首选项的值时,我的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)
答案 0 :(得分:1)
尝试将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);
答案 1 :(得分:1)
夸大布局和导航视图
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());