0

I'm trying to set a a text for my TextView which is inside of DrawerLayout, the problem is, everytime that I try to set a text I got a NullException, maybe the problem is because I'm not checking if it is open. I'm not doing nothing out of the ordinary...

private TextView my_textView;

onCreate.....

Drawer class...

my_textView = (Textview)findViewByid(R.id.nav_text);

enter image description here

Thanks!

3 Answers3

3

I'm assuming the text is inside the NavigationView header.

First, Initialize your NavigationView header

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

Next, Initialize the view inside the header.

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

Now, Change the text by using setText method

my_textView.setText("New Text");

Hope this helps.

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
0

You need to call drawerLayout.findViewByid() instead of findViewByid(). Because the textview is available in the drawerLayout. So change your code as below

private TextView my_textView;

onCreate.....

Drawer class...

my_textView = (Textview)drawerLayout.findViewByid(R.id.nav_text);
Madhan
  • 555
  • 5
  • 23
0

Try like below if you want to access header views :

  View headerView = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
        navigationView.addHeaderView(headerView);

        TextView tvUserName = (TextView) headerView.findViewById(R.id.admin_header_name_tv);
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33