Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase aute null object reference in android

I need some help with my android app. I started to work with google firebase and my app crash every time on the launcher activity, I get null from firebase object , please help !


Error:

    E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.workthree.todo, PID: 4011
              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$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Code SplashScreen.java (Launcher Activity)

public class SplashScreen extends AppCompatActivity {

private final static String TAG = SplashScreen.class.getSimpleName();
private static final String  USER_IS_LOGIN= "UserIsLogin";
private static final String  UI_ID_FIREBASE= "UiIdFirebase";
// Duration of wait
private final int SPLASH_DISPLAY_LENGTH = 2000;

private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseAuth mAuth;
ToDoApplication mApplication = ToDoApplication.getApplicationInstance();
private SharedPreferences prefs = mApplication.getApplicationPreferences();
private  SharedPreferences.Editor editor = prefs.edit();
private User mUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    mAuth = FirebaseAuth.getInstance();
    mUser = User.getInstance();

    // New Handler to start the Menu-Activity and close this Splash-Screen after some seconds.
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            Logger.d("Start splash screen");
            mAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                   if (user != null) {
                        // User is signed in
                        mUser.setLoginState(true);
                       mUser.setUiIdFirebase(user.getUid());
                        editor.putBoolean(USER_IS_LOGIN,true);
                        editor.putString(UI_ID_FIREBASE,user.getUid());
                        editor.commit();
                    Log.d(TAG, "User state : signed_in:" + user.getUid());
                        StartMainActivity();
                        SplashScreen.this.finish();
                   // } else {
                        // User is signed out
                        mUser.setLoginState(false);
                        editor.putBoolean(USER_IS_LOGIN,false);
                        editor.commit();
                        Log.d(TAG, "User state : signed_out");
                        StartSignInActivity();
                        SplashScreen.this.finish();
                    }
                }
            };

        }
    }, SPLASH_DISPLAY_LENGTH);
}

public void StartMainActivity (){
    Log.d(TAG,"User is in , Start MainActivity");
    Intent i = new Intent(SplashScreen.this,MainActivity.class);
    startActivity(i);
}

public void StartSignInActivity (){
    Log.d(TAG,"User need to sign in , Start SignInActivity");
    Intent i = new Intent(SplashScreen.this,SignInActivity.class);
    startActivity(i);
}

@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
protected void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

}

like image 875
Daniel Levi Avatar asked Dec 19 '16 10:12

Daniel Levi


1 Answers

Your mAuthListener is null at onStart method. Add listener at your Handler.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
mAuth = FirebaseAuth.getInstance();
mUser = User.getInstance();

// New Handler to start the Menu-Activity and close this Splash-Screen after some seconds.
new Handler().postDelayed(new Runnable(){
    @Override
    public void run() {
        Logger.d("Start splash screen");
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
               if (user != null) {
                    // User is signed in
                    mUser.setLoginState(true);
                   mUser.setUiIdFirebase(user.getUid());
                    editor.putBoolean(USER_IS_LOGIN,true);
                    editor.putString(UI_ID_FIREBASE,user.getUid());
                    editor.commit();
                Log.d(TAG, "User state : signed_in:" + user.getUid());
                    StartMainActivity();
                    SplashScreen.this.finish();
               } else {
                    // User is signed out
                    mUser.setLoginState(false);
                    editor.putBoolean(USER_IS_LOGIN,false);
                    editor.commit();
                    Log.d(TAG, "User state : signed_out");
                    StartSignInActivity();
                    SplashScreen.this.finish();
                }
            }
        };
        //add listener
        mAuth.addAuthStateListener(mAuthListener);
    }
  }, SPLASH_DISPLAY_LENGTH);
}

Delete listener in your onStart method.

@Override
protected void onStart() {
super.onStart();
// delete listeneradd mAuth.addAuthStateListener(mAuthListener);
}
like image 183
eurosecom Avatar answered Nov 05 '22 10:11

eurosecom