Problems trying to change a nav_header_main.xml text view

Asked

Viewed 1,146 times

0

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

Em vermelho é o texto que quero alterar

In red is the text I want to change.

Below is the xml generated automatically by android studio, I just added an ID to this Textview:

TextView
   android: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 onCreate of the Mainactivity.java class I made the following codes:

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

   txtUsuarioLogado.setText("Nome Alterado");

However, Textview is always getting null value, locking the application. How would be the correct way to manipulate it

1 answer

2

Assuming that the lock you refer to is a NullPointerException, the method findViewById is not finding the ID you reported. This is because the informed id does not belong to the hierarchy of the searched layout. Therefore, the txtUsuarioLogado should not be at 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");   
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.