我在Android应用程序中为LogIn(Firebase)制作了两种方法。其中一个电子邮件/密码工作正常,Facebook登录在Facebook登录2次后总是崩溃。我无法找到问题出在哪里,总是在我第三次清除缓存后崩溃。在2次登录Facebook崩溃后,我登录和退出电子邮件/通行证的次数并不重要。
电子邮件/密码登录
private void signIn() {
String UserEmail = mEditUsername.getText().toString().trim();
String UserPassword = mEditPassword.getText().toString().trim();
if (TextUtils.isEmpty(UserEmail)) {
Toast.makeText(this, "Please enter your username", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(UserPassword)) {
Toast.makeText(this, "Please enter your password", Toast.LENGTH_SHORT).show();
return;
}
mSignInPDialog = new ProgressDialog(LogInActivity.this);
setupPDialog(mSignInPDialog,"Loading...","Logging in");
mFirebaseAuth.signInWithEmailAndPassword(UserEmail, UserPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
setLoggedIn("True");
mSignInPDialog.dismiss();
startActivity(new Intent(LogInActivity.this, MainActivity.class));
Toast.makeText(LogInActivity.this, "Logged in succesfully!", Toast.LENGTH_SHORT).show();
finish();
} else {
FirebaseException e = (FirebaseException) task.getException();
mSignInPDialog.dismiss();
Toast.makeText(LogInActivity.this, "LogIn failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
Facebook登录
private void facebookLogin()
{
mSignInPDialog = new ProgressDialog(LogInActivity.this);
setupPDialog(mSignInPDialog,"Loading...","Getting data");
LoginManager.getInstance().logInWithReadPermissions(LogInActivity.this, Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
setLoggedIn("FacebookOnly");
mSignInPDialog.dismiss();
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(LogInActivity.this,"Log in was canceled!",Toast.LENGTH_SHORT).show();
mSignInPDialog.dismiss();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(LogInActivity.this,"Error" + error.getMessage(),Toast.LENGTH_SHORT).show();
mSignInPDialog.dismiss();
}
});
}
private void handleFacebookAccessToken(AccessToken token) {
mSignInFPDialog = new ProgressDialog(LogInActivity.this);
setupPDialog(mSignInFPDialog,"Loading...","Logging in");
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mFirebaseAuth.getCurrentUser();
setLoggedIn("True");
mSignInFPDialog.dismiss();
startActivity(new Intent(LogInActivity.this, MainActivity.class));
Toast.makeText(LogInActivity.this, "Logged in succesfully!", Toast.LENGTH_SHORT).show();
finish();
} else {
FirebaseException e = (FirebaseException) task.getException();
LoginManager.getInstance().logOut();
mSignInFPDialog.dismiss();
Toast.makeText(LogInActivity.this, "LogIn failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
退出 - MainActivity
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
Toast.makeText(MainActivity.this,"asdasdasd",Toast.LENGTH_SHORT).show();
return;
}
firebaseAuth.signOut();
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
更新v2
崩溃日志
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length