Skip to main content
se eliminaron 75 caracteres en el cuerpo
Origen Enlace
RRGT19
  • 2k
  • 10
  • 37
  • 63
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        FirebaseUser user = mAuth.getCurrentUseraddAuthStateListener();
        ifnew FirebaseAuth.AuthStateListener(user != null) {
            Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();@Override
         public void onAuthStateChanged user.reload().addOnSuccessListener(new@NonNull OnSuccessListener<Void>()FirebaseAuth firebaseAuth){
                @Override
                public voidif onSuccess(VoidmAuth.getCurrentUser() aVoid!= null) {
                    // ....Toast.makeText(HomeActivity.
           this, "User is not null", }Toast.LENGTH_SHORT).show();
            });
        } else {
            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
        }
    }
});

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

}
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        FirebaseUser user = mAuth.getCurrentUser();
        if (user != null) {
            Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
            user.reload().addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    // ......
                }
            });
        } else {
            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

}
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener(){
        @Override
        public void onAuthStateChanged (@NonNull FirebaseAuth firebaseAuth){
            if (mAuth.getCurrentUser() != null) {
                Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
            } else {
            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
        }
    }
});

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

}
se eliminaron 159 caracteres en el cuerpo
Origen Enlace
RRGT19
  • 2k
  • 10
  • 37
  • 63

java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.androidfirebase.gmsauth.tasksFirebaseAuth$AuthStateListener.Task comonAuthStateChanged(com.google.firebase.auth.FirebaseUser.reload(FirebaseAuth)' on a null object reference

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        mAuthListenerFirebaseUser =user new= FirebaseAuthmAuth.AuthStateListenergetCurrentUser() {
            @Override;
            public voidif onAuthStateChanged(@NonNulluser FirebaseAuth!= firebaseAuthnull) {
               Toast.makeText(HomeActivity.this, //"User Notaris quenot hagonull", reloadToast.LENGTH_SHORT).show();
                firebaseAuth.getCurrentUser()user.reload().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        FirebaseUser user =// mAuth.getCurrentUser();.....
                        if (user == null)}
                            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show(});
                       } else
                {
            Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    } 

}

com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseUser.reload()' on a null object reference

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                // Notar que hago reload()
                firebaseAuth.getCurrentUser().reload().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        if (user == null)
                            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }
}

java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged(com.google.firebase.auth.FirebaseAuth)' on a null object reference

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        FirebaseUser user = mAuth.getCurrentUser();
        if (user != null) {
            Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
            user.reload().addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    // ......
                }
            });
        } else {
            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    } 

}
Origen Enlace
RRGT19
  • 2k
  • 10
  • 37
  • 63

Error al implementar FirebaseAuth AuthStateListener

Me gustaría conseguir el siguiente flujo en mi App:

  1. El usuario se registra, valida email y luego inicia sesión
  2. Una vez dentro, si elimino la cuenta desde la consola de Firebase, redirigir al usuario a MainActivity (porque obviamente ya no tiene una cuenta)

Tengo todo, menos lo de verificar si el usuario es null. He tratado de hacerlo pero, el App se cierra y recibo el error:

com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseUser.reload()' on a null object reference

A parte de esto, para que aparezca el Toast, debo ir a otro Activity y luego volver para que se ejecute el Listener. No se supone que funcione dentro del mismo Activity?

La idea es siempre revisar si el usuario que está en el App es válido o no (ejemplo, si ha sido eliminado o si la cuenta ha sido desactivada).

Código:

public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                // Notar que hago reload()
                firebaseAuth.getCurrentUser().reload().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        if (user == null)
                            Toast.makeText(HomeActivity.this, "User is null", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(HomeActivity.this, "User is not null", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }
}

Estoy usando:

  1. Android Studio 3.1.4
  2. implementation 'com.google.firebase:firebase-auth:16.0.3'

Alguna idea de el por qué no me funciona?