M
M
max_vr2016-06-19 14:28:56
Java
max_vr, 2016-06-19 14:28:56

How to access the text fields in the sidebar header?

I am new to android programming. How do I access these 2 textboxes in the sidebar header of the navigation drawer activity? 4d7899450f2a42d6b2caca0da044bd75.bmp
It doesn't work in the standard way:

TextView hello;
hello = (TextView) findViewById (R.id.hello);
hello.setText("Hello world!");

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Superiorr, 2016-10-12
@Superiorr

android

//задаем navigationView
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        //задаем его шапку
        View header_nv = navigationView.inflateHeaderView(R.layout.nav_header_main);
        //нужный нам элемент
        TextView hello = (TextView) header_nv.findViewById(R.id.hello);
        // и собственно изменяем его
        hello.setText("Hello world!");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question