导航视图标题上的SetText

时间:2017-04-09 10:23:45

标签: android navigationview

我正在使用firebase auth身份验证和导航抽屉布局,我需要的是在导航的标题上设置用户电子邮件,事情是我得到的,是它找不到相关的textView在我的布局上,因为主要活动与我的主要布局相关联而不是导航本身,我做了一个测试,看看用户是否从数据库中检索到了,我收到了电子邮件,但是当我这样做时:

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

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

我得到一个NullPointerException,我认为这与userTxt有关,所以如果有人经历过它,你们可以给我一个如何解决这个问题的提示吗?

栈跟踪

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)

1 个答案:

答案 0 :(得分:1)

从例外情况可以清楚地看出,您的userTxt为空。

要访问NavigationView标题,您需要先获取标题视图:

来自Documentation

  

View getHeaderView (int index)获取指定的标题视图   位置。

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);