I'm having problems when I try to set the Name of the user and its email in the DrawerLayout header. Here you can see how it must be. (This is the main class, where all is ok) The name on top and then the email of the currentUser The user has previosuly logged in. I tried this in the new class "PerfilActivity", where the drawerlayout is present too but it keeps crashing. Any advice would be great. Thanks
package com.Trueque;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
/**
* En esta clase se va aplicar la lógica de la ventana de Perfil, que va a permitir actualizar los datos de los usuarios.
* Cuando se pinche sobre contraseña, se va a pedir que se escriba la contraseña actual, luego una vez la contraseña nueva y por ultimo que se
* confirme esta contraseña
*/
public class PerfilActivity extends AppCompatActivity {
TextView nombre_cabecera, descripcion_cabecera;
TextInputLayout nombre, apellido, telefono, email;
FirebaseAuth mAuth;
FirebaseDatabase mDatabase;
FirebaseUser mCurrentUser;
TruequeDB db;
NavigationView navigationView;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.perfil_user_layout);
nombre = findViewById(R.id.nombrePerfil);
apellido = findViewById(R.id.apellidoPerfil);
telefono = findViewById(R.id.telefonoPerfil);
email = findViewById(R.id.emailPerfil);
nombre_cabecera = findViewById(R.id.nombre_cabecera);
descripcion_cabecera = findViewById(R.id.descripcion_cabecera);
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
if (mCurrentUser != null) {
descripcion_cabecera.setText(mCurrentUser.getEmail());
nombre_cabecera.setText(mCurrentUser.getDisplayName());
//img_perfil.setImageURI(currentUser.getPhotoUrl());
System.out.println(mCurrentUser.getEmail());
System.out.println(mCurrentUser.getDisplayName());
} else {
nombre_cabecera.setText("Invitado");
}
///////////////////
// TOOLBAR //
///////////////////
Toolbar toolbar = findViewById(R.id.toolbar);
//Setamos una app bar propia, y como ya tenemos nuestro menu contextual, no hace falta más.
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
///////////////////
// DRAWER LAYOUT //
///////////////////
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, 0, 0);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
/**
* Seleccion de la opcion de drawerLayout, gracias al listener se obtiene la referencia del objeto pulsado,
* en el switch se hace una acción para cada caso.
*/
navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.info_usuario:
Toast.makeText(PerfilActivity.this, "Ya estamos aquí", Toast.LENGTH_LONG).show();
System.out.println("Entra en el case de info del usuario");
if (mCurrentUser == null) {
Toast.makeText(PerfilActivity.this, "Se requiere inicio de sesión", Toast.LENGTH_LONG).show();
} else {
}
break;
case R.id.inicio:
Intent inicio = new Intent(PerfilActivity.this, MainActivity.class);
startActivity(inicio);
break;
case R.id.subir_item:
Toast.makeText(PerfilActivity.this, "Ya estamos aquí", Toast.LENGTH_LONG).show();
break;
case R.id.cerrar_sesion:
System.out.println("Entra en el case de cerrar sesión");
logOut();
break;
case R.id.ajustes:
System.out.println("Entra en el case de ajustes");
break;
default:
throw new IllegalArgumentException("menu option not implemented!!");
}
return false;
}
});
}
private void logOut() {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
This is my XML file of the class mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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="end">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
This part of code is irrelevant
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/cabecera_menu" <-- My layoutfile
app:menu="@menu/menu_drawer" /> <-- My menu inside de DrawerLayout
</androidx.drawerlayout.widget.DrawerLayout>
My layoutFile cabecera_menu :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:showIn="navigation_view"
android:background="@color/fondoBlueLight"
android:id="@+id/cabecera">
<ImageView
android:id="@+id/img_cabecera"
android:layout_width="149dp"
android:layout_height="141dp"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.431"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/perfil_user" />
<TextView
android:id="@+id/nombre_cabecera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textAlignment="textStart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/img_cabecera"
app:layout_constraintStart_toStartOf="@+id/img_cabecera"
app:layout_constraintTop_toBottomOf="@+id/img_cabecera" />
<TextView
android:id="@+id/descripcion_cabecera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
android:textAlignment="textStart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/nombre_cabecera"
app:layout_constraintStart_toStartOf="@+id/nombre_cabecera"
app:layout_constraintTop_toBottomOf="@+id/nombre_cabecera" />
</androidx.constraintlayout.widget.ConstraintLayout>
here you can see the header layout visualization
here is the portion of code where is the error :
if (mCurrentUser != null) {
descripcion_cabecera.setText(mCurrentUser.getEmail()); <-- Line 57
nombre_cabecera.setText(mCurrentUser.getDisplayName()); <--This line do not work either
//img_perfil.setImageURI(currentUser.getPhotoUrl());
System.out.println(mCurrentUser.getEmail());
System.out.println(mCurrentUser.getDisplayName());
} else {
nombre_cabecera.setText("Invitado");
}