2016-12-22 2 views
0

私はこれにしばらく苦しんでおり、このトピックに関して他の質問から多くの解決策を試しました。基本的には、onCompleteの結果をfalseにして、mAuthオブジェクトをnullにするために間違ったことを知る必要があります。FirebaseのFacebook認証後にmAuthがヌルです

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    setContentView(R.layout.activity_login); 

    email = (EditText) findViewById(R.id.txtEmail); 
    password = (EditText) findViewById(R.id.txtPassword); 

    mAuth = FirebaseAuth.getInstance(); 
    mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user != null) { 
       // User is signed in 
       Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 
      } else { 
       // User is signed out 
       Log.d(TAG, "onAuthStateChanged:signed_out"); 
      } 
     } 
    }; 

    googleSignInBtn = (SignInButton) findViewById(R.id.google_login); 
    googleSignInBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      authGoogle(); 
     } 
    }); 

    mCallbackManager = CallbackManager.Factory.create(); 
    facebookLoginBtn = (LoginButton) findViewById(R.id.facebook_login_button); 
    facebookLoginBtn.setReadPermissions("email", "public_profile"); 
    facebookLoginBtn.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 

      Log.d(TAG, "facebook:onSuccess: " + loginResult); 
      handleFacebookAccessToken(loginResult.getAccessToken()); 
     } 

     @Override 
     public void onCancel() { 

      Log.d(TAG, "facebook:onCancel"); 
      Toast.makeText(LoginActivity.this, "Facebook login cancelled", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onError(FacebookException error) { 

      Log.d(TAG, "facebook:onError", error); 
      Toast.makeText(LoginActivity.this, "Facebook login error", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestIdToken(getString(R.string.default_web_client_id)) 
      .requestEmail() 
      .build(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 

    mUserReference = mRootReference.child("User"); 


} 


    private void handleFacebookAccessToken(AccessToken token) { 
    Log.d(TAG, "handleFacebookAccessToken:" + token); 

    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken()); 
    mAuth.signInWithCredential(credential) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); 
        Toast.makeText(LoginActivity.this, "Welcome "+mAuth.getCurrentUser().getDisplayName(), Toast.LENGTH_SHORT).show(); 
        Intent i = new Intent(LoginActivity.this, MainActivity.class); 
        writeNewAuthenticatedUser(mAuth.getCurrentUser()); 
        startActivity(i); 
        signOut(); 
        // If sign in fails, display a message to the user. If sign in succeeds 
        // the auth state listener will be notified and logic to handle the 
        // signed in user can be handled in the listener. 
        if (!task.isSuccessful()) { 
         Log.w(TAG, "signInWithCredential", task.getException()); 
         Toast.makeText(LoginActivity.this, "Authentication failed.", 
           Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
} 

ログイン:

D/Login: onAuthStateChanged:signed_out 
    D/Login: facebook:onSuccess: [email protected] 
    D/Login: handleFacebookAccessToken:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, email]} 

エラー:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getDisplayName()' on a null object reference 

答えて

1

mAuthがnullではありません。 mAuth.getCurrentUser()がnullを返しています。これは、おそらくsignInWithCredentialによって返されたタスクが正常に完了しなかったことを意味します。 addOnCompleteListenerを使用する場合は、タスクが正常に完了したことを確認する必要があります。利用可能なデータ(現在のユーザー)にアクセスするだけです。

+0

応答に感謝します。タスクを正しく返す方法に関するアドバイスはありますか?なぜそれがnullを返すのか不思議です。この方法は、 – mapage2

+0

のGoogleサインで動作します。タスクは成功しているかどうかです。成功しない場合は、例外を見てください。なぜtask.getException()の値を記録しないのですか? –

+0

私はそれを実装することを確認します。私はそれが役に立つと確信しています。ユーザーの電子メール、表示名、Firebase ID(Facebookの認証サービスを使用)の値にアクセスしたい場合は、アドバイスが必要でしょうか。 – mapage2

関連する問題