Tengo problemas a la hora de usar algunos datos de Firebase despues de hacer login con una cuenta de Google. La idea es coger el nombre, correo e imagen del usuario para luego mostrarlo, pero a la hora se hacer un .setText(firebaseUser.getDisplayName) y demas me salta el error NullPointerException.
Unable to start activity ComponentInfo{com.example.musicae/com.example.musicae.View.NavigationBarWithTabsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.musicae.View.NavigationBarWithTabsActivity.setUser(NavigationBarWithTabsActivity.java:82) at com.example.musicae.View.NavigationBarWithTabsActivity.onCreate(NavigationBarWithTabsActivity.java:46)
Me deja iniciar sesion y me deja coger los datos ya que he hecho un Log.d y me muestra el Logcat.
Log.d("MiAPP", firebaseUser.getDisplayName());
Log.d("MiAPP", firebaseUser.getEmail());
Log.d("MiAPP", firebaseUser.getPhotoUrl().toString());
Mi código, NavigationBarWithTabsActivity.java:
public class NavigationBarWithTabsActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public ImageView userImage;
public TextView userName;
public TextView userEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_bar_with_tabs);
setUp();
}
public void setUp() {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
TapPagerAdapter tapPagerAdapter = new TapPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tapPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
setUser();
}
public void setUser(){
userImage = findViewById(R.id.userimage);
userName = findViewById(R.id.username);
userEmail = findViewById(R.id.useremail);
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
Log.d("MiAPP", firebaseUser.getDisplayName());
Log.d("MiAPP", firebaseUser.getEmail());
Log.d("MiAPP", firebaseUser.getPhotoUrl().toString());
userName.setText(firebaseUser.getDisplayName());
userEmail.setText(firebaseUser.getEmail());
Glide.with(this)
.load(firebaseUser.getPhotoUrl().toString())
.into(userImage);
findViewById(R.id.logoutBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AuthUI.getInstance()
.signOut(NavigationBarWithTabsActivity.this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
finish();
}
});
}
});
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
goToReportSection();
}
if (id == R.id.action_help) {
goToHelpSection();
}
return super.onOptionsItemSelected(item);
}
public void goToHelpSection(){
Intent intent = new Intent(getApplicationContext(), SlideActivity.class);
startActivity(intent);
}
public void goToReportSection(){
Intent intent = new Intent(getApplicationContext(), ReportActivity.class);
startActivity(intent);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_music) {
Intent intent = new Intent(getApplicationContext(), NavigationBarWithTabsActivity.class);
startActivity(intent);
} else if (id == R.id.nav_account) {
Intent intent = new Intent(getApplicationContext(), AccountActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
LoginActivity:
public class LoginActivity extends AppCompatActivity {
private static final int RC_SIGN_IN = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.signInBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
comeIn();
}
void comeIn(){
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
startActivity(new Intent(this, NavigationBarWithTabsActivity.class));
finish();
}
}
void signIn(){
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(Arrays.asList(
new AuthUI.IdpConfig.GoogleBuilder().build()))
.build(),
RC_SIGN_IN);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
comeIn();
}
}
}
}
activity_navigation_bar_with_tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<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/app_bar_main"
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"
app:itemTextColor="@color/colorWhite"
app:itemIconTint="@color/colorWhite"
android:background="@color/backgroundMenu"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
<Button
android:id="@+id/logoutBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/buttonBlue"
android:textStyle="bold"
android:text="@string/log_out"
android:textColor="@color/colorWhite" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
nav_header_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:backgroundTint="@color/colorPrimaryDark"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/userImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/userEmail"
android:layout_width="wrap_content"
android:textColor="@color/colorWhite"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />
</LinearLayout>
Estos son todos los Layout que tengo: