我的代码有问题。错误是:
FATAL EXCEPTION: main
Process: bike.ellos.ellos, PID: 22835
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
at com.google.firebase.auth.FirebaseAuth$2.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
SplashScreen.class代码是:
public class SplashScreen extends Activity {
private int counter = 0;
private Integer pontos;
private String userId;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
mAuth = FirebaseAuth.getInstance();
IniciaSplash();
}
//FUNÇÃO QUE CONTA OS SEGUNDO E MUDA A TELA
public void IniciaSplash(){
new Thread(new Runnable() {
@Override
public void run() {
counter++;
try{
while (counter==1 || counter<3)//TEMPO DE ESPERA DO CONTADOR "5"
{
Thread.sleep(1000);
counter++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (counter==3){
//ACRESCE UM NO CONTADOR PARA NÃO VIRAR UM LOOP INFINITO
counter++;
UserCheck();
}
}
}).start();
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void UserCheck(){
///Checa se o usuário ja esta logado
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
//Se o usuário já estiver logado
if(firebaseAuth.getCurrentUser() == null){
startActivity(new Intent(SplashScreen.this,Register.class));
SplashScreen.this.finish();
}else{
userDataLoged();
getRealtimeScore();
}
}
};
}
private void userDataLoged(){
userId = firebaseUser.getUid();
}
//Pega os pontos no Firebase Realtime
private void getRealtimeScore(){
// Verifica os dados no Firebase Realtime
mFirebaseDatabase.child(userId).child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//verifica se ta cadastrado corretamente
if (dataSnapshot != null){
//Atribui o valor do "score" aos pontos
pontos = dataSnapshot.getValue(Integer.class);
//Se os pontos forem "NULL" é pq o usuário não registrou completamente(PersonalRegistration)
if(pontos == null){
startActivity(new Intent(SplashScreen.this, PersonalRegistration.class));
SplashScreen.this.finish();
finish();
Toast.makeText(SplashScreen.this, "Finalize o cadastro" , Toast.LENGTH_LONG).show();
}else {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
SplashScreen.this.finish();
finish();
}
}else{
mAuth.signOut();
}
}
@Override
public void onCancelled(DatabaseError error) {
Toast.makeText(SplashScreen.this, "onCancelled" , Toast.LENGTH_LONG).show();
}
});
}
}
应用程序无法打开。所以我运行应用程序,它崩溃了。 我尝试了很多东西,但我还没有解决方案。 有人会有任何解决方案吗?