2016-12-19 14 views
2

私のAndroidアプリケーションでいくつかの助けが必要です。 Googleのfirebaseで動作するようになり、起動時に毎回アプリがクラッシュしてしまいました。私はfirebaseオブジェクトからヌルを取得しています。助けてください!アンドロイドのFirebase aute nullオブジェクトリファレンス


エラー:

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) 

コードSplashScreen.java(ランチャー活性)

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); 
    } 
} 

}

答えて

5

あなたmAuthListenerがONSTART方法でヌルです。 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); 
} 

あなたのonStartメソッドのリスナーを削除します。

@Override 
protected void onStart() { 
super.onStart(); 
// delete listeneradd mAuth.addAuthStateListener(mAuthListener); 
} 
関連する問題