无法访问nav_header_main布局视图

时间:2016-12-05 12:27:04

标签: android navigation-drawer navigationview

我无法访问nav_header_main layout

中的视图
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    //setting profile picture in nav
    View emailview=navigationView.getHeaderView(2);
    View picview=navigationView.getHeaderView(0);
    View nameview=navigationView.getHeaderView(1);
    email_textview=(TextView)emailview.findViewById(R.id.emailtextView);
    profile_image=(ImageView)picview.findViewById(R.id.profile_imageView);
    username_textview=(TextView)nameview.findViewById(R.id.appbarusernametextview);
    Intent intent =new Intent(this,EmailPasswordActivity.class);
    email=this.getIntent().getExtras().getString("email");
    email_textview.setText(email);

当我使用headerinflater它可以工作,但添加了很多标题

3 个答案:

答案 0 :(得分:4)

View headerView = navigationView.getHeaderView(0)
email_textview = (TextView)headerView.findViewById(R.id.emailtextView);
profile_image = (ImageView)headerView(R.id.profile_imageView);
username_textview = (TextView)headerView(R.id.appbarusernametextview);

试试这个希望它会帮助你

答案 1 :(得分:2)

因为你做错了方法改变这段代码

View emailview=navigationView.getHeaderView(2);
View picview=navigationView.getHeaderView(0);
View nameview=navigationView.getHeaderView(1);
email_textview=(TextView)emailview.findViewById(R.id.emailtextView);
profile_image=(ImageView)picview.findViewById(R.id.profile_imageView);
username_textview=(TextView)nameview.findViewById(R.id.appbarusernametextview);

到这个

 email_textview=(TextView) navigationView.getHeaderView(0).findViewById(R.id.emailtextView);
 profile_image=(ImageView)navigationView.getHeaderView(0).findViewById(R.id.profile_imageView);
 username_textview=(TextView)navigationView.getHeaderView(0).findViewById(R.id.appbarusernametextview);

答案 2 :(得分:0)

用于访问NavigationView标题子项:

TextView lblUserName = (TextView)navigationView.getHeaderView(0).findViewById(R.id.lblUserName);