Problems trying to change a text view of nav_header_main.xml

0

I am working for the first time with the Navigation Drawer Activity, and would like to change (by java) the text that appears in the menu nav_header_main.xml

Inredit'sthetextthatIwanttochange.

Belowisthexmlautomaticallygeneratedbyandroidstudio,IjustaddedanIDtothisTextView:

TextViewandroid:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:paddingTop="@dimen/nav_header_vertical_spacing"
   android:text="Android Studio"
   android:id="@+id/txtUsuarioLogado"
   android:textAppearance="@style/TextAppearance.AppCompat.Body1"

So, in the onCreate class MainActivity.java I made the following codes:

   TextView txtUsuarioLogado = (TextView) findViewById(R.id.txtUsuarioLogado);

   txtUsuarioLogado.setText("Nome Alterado");

However, the TextView is always getting null, locking the application. How would the correct way of manipulating it

    
asked by anonymous 17.11.2017 / 01:08

1 answer

1

Assuming that the lock you're referring to is a NullPointerException , the findViewById method is not finding the ID you entered. This is because the ID you entered does not belong to the hierarchy of the searched layout. Therefore, txtUsuarioLogado should not be on the same level as the activity layout.

I suggest checking the layouts and structuring them as follows:

main_activity.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/main_activity" <--- o layout de conteúdo da activity
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/menu_header" <--- o layout do cabeçalho
        app:menu="@menu/menu_sidebar" <--- as opções do menu
    />

</android.support.v4.widget.DrawerLayout>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    // Obtém a referência do layout de navegação
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    // Obtém a referência da view de cabeçalho
    View headerView = navigationView.getHeaderView(0);

    // Obtém a referência do nome do usuário e altera seu nome
    TextView txtUsuarioLogado = (TextView) headerView.findViewById(R.id.txtUsuarioLogado);
    txtUsuarioLogado.setText("Nome Alterado");   
}
    
28.11.2017 / 18:30