导航视图上的 SetText header
SetText on navigation view header
我正在使用 firebase 身份验证和导航抽屉布局,我需要的是在我的导航 header 上设置用户电子邮件,就像我得到的那样在我的布局上找不到相关的 textView,因为主要 activity 与我的主要布局相关联,而不是导航本身,我做了一个测试,看看用户是否从数据库中检索,我收到了电子邮件,但是当我这样做时:
auth = FirebaseAuth.getInstance();
String email = auth.getCurrentUser().getEmail();
Log.d("email",email);
if (auth.getCurrentUser() != null) {
userTxt.setText(email);
}
我得到一个 NullPointerException
,我认为它与 userTxt 相关,所以如果有人以前遇到过它,你们能告诉我如何解决这个问题吗?
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.access0(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.access0(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)
从异常中可以清楚地看出您的 userTxt
为空。
要访问 NavigationView
Header -- 您需要先获取 Header 视图:
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);
我正在使用 firebase 身份验证和导航抽屉布局,我需要的是在我的导航 header 上设置用户电子邮件,就像我得到的那样在我的布局上找不到相关的 textView,因为主要 activity 与我的主要布局相关联,而不是导航本身,我做了一个测试,看看用户是否从数据库中检索,我收到了电子邮件,但是当我这样做时:
auth = FirebaseAuth.getInstance();
String email = auth.getCurrentUser().getEmail();
Log.d("email",email);
if (auth.getCurrentUser() != null) {
userTxt.setText(email);
}
我得到一个 NullPointerException
,我认为它与 userTxt 相关,所以如果有人以前遇到过它,你们能告诉我如何解决这个问题吗?
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.access0(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.access0(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)
从异常中可以清楚地看出您的 userTxt
为空。
要访问 NavigationView
Header -- 您需要先获取 Header 视图:
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);