我已经成功实施了Google登录,它运行正常,当我实际完成整个登录过程时,我没有例外。
但是,如果我点击Google登录按钮然后点击后退按钮以关闭对话框,我会以某种方式收到以下错误:
引起:com.google.android.gms.tasks.RuntimeExecutionException:com.google.android.gms.common.api.ApiException:12501: 在com.google.android.gms.tasks.zzu.getResult(未知来源:17)
指向我这段代码:
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.result
firebaseAuthWithGoogle(account)
} catch (e: ApiException) {
Toast.makeText(this, "Google Sign In Failed", Toast.LENGTH_SHORT).show()
}
}
具体在val account = task.result
行。
我的问题是,不应该通过catch
单独处理它并打印出吐司吗?为什么整个应用程序崩溃呢?
这是我的整个SignInActivity
:
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import com.dancam.subscriptions.ActiveSubscriptions.SubscriptionsMain
import com.dancam.subscriptions.R.id.google_signin_button
import com.dancam.subscriptions.R.id.signin_progressbar
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.SignInButton
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.reset_password_dialog.view.*
import kotlinx.android.synthetic.main.sign_dialog.view.*
import kotlinx.android.synthetic.main.signin.*
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.sdk25.coroutines.onClick
class SignInActivity: AppCompatActivity() {
private var mAuth: FirebaseAuth? = null
private var mGoogleSignInClient: GoogleSignInClient? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signin)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
mAuth = FirebaseAuth.getInstance()
google_signin_button.setSize(SignInButton.SIZE_WIDE)
google_signin_button.setOnClickListener { signIn() }
}
override fun onStart() {
super.onStart()
val currentUser = mAuth?.currentUser
if (currentUser!=null) loggedIn(currentUser)
}
private fun signIn() {
val signInIntent = mGoogleSignInClient?.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
signin_progressbar.visibility = View.VISIBLE
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.result
firebaseAuthWithGoogle(account)
} catch (e: ApiException) {
Toast.makeText(this, "Google Sign In Failed", Toast.LENGTH_SHORT).show()
}
}
}
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
mAuth?.signInWithCredential(credential)
?.addOnCompleteListener {
if (it.isSuccessful) {
signin_progressbar.visibility = View.GONE
val user = mAuth?.currentUser
loggedIn(user)
} else {
signin_progressbar.visibility = View.GONE
Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
loggedIn(null)
}
}
}
fun onSignUpClicked(args: View) {
val view = layoutInflater.inflate(R.layout.sign_dialog, null)
val dialog = BottomSheetDialog(this)
dialog.setContentView(view)
dialog.show()
view.register_button.setOnClickListener {
val email = view.signup_email.text.toString()
val password = view.signup_password.text.toString()
val password2 = view.signup_password2.text.toString()
if (password == password2) {
mAuth?.createUserWithEmailAndPassword(email, password)
?.addOnCompleteListener {
if (it.isSuccessful) {
//sign in success updateUI
val user = mAuth?.currentUser
loggedIn(user)
} else {
// signin failed
//TODO: translate
Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
loggedIn(null)
}
}
} else {
//TODO: translate
Toast.makeText(this, "Passwords do not match", Toast.LENGTH_LONG).show()
}
}
}
fun onSignInClicked(args: View) {
val view = layoutInflater.inflate(R.layout.sign_dialog, null)
val dialog = BottomSheetDialog(this)
//TODO: translate
view.dialog_textview.text = "Sign In"
view.signup_password2.visibility = View.GONE
dialog.setContentView(view)
dialog.show()
//TODO: translate
view.register_button.text = "Sign In"
view.register_button.setOnClickListener {
val email = view.signup_email.text.toString()
val password = view.signup_password.text.toString()
mAuth?.signInWithEmailAndPassword(email, password)
?.addOnCompleteListener {
if (it.isSuccessful) {
val user = mAuth?.currentUser
loggedIn(user)
} else {
//TODO: translate
Toast.makeText(this, "Sign In Failed", Toast.LENGTH_LONG).show()
view.forgotten_password.visibility = View.VISIBLE
view.forgotten_password.onClick {
dialog.dismiss()
resetDialog()
}
}
}
}
}
private fun resetDialog() {
val resetView = layoutInflater.inflate(R.layout.reset_password_dialog, null)
val resetDialog = BottomSheetDialog(this)
resetDialog.setContentView(resetView)
resetDialog.show()
resetView.send_reset_email.setOnClickListener {
resetView.send_reset_email.isClickable = false
val email = resetView.reset_email.text.toString()
mAuth?.sendPasswordResetEmail(email)
?.addOnSuccessListener {
//TODO: translate
Toast.makeText(this, "Email Sent", Toast.LENGTH_SHORT).show()
resetDialog.dismiss()
}
}
}
private fun loggedIn(user: FirebaseUser?) {
startActivity(Intent(this, SubscriptionsMain::class.java))
finish()
}
fun onSkipClicked(args:View) {
loggedIn(null)
}
companion object {
const val RC_SIGN_IN = 0000 }
}
答案 0 :(得分:5)
你可以尝试一下,让我知道会发生什么:
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.result(ApiException::class.java)
firebaseAuthWithGoogle(account)
} catch (e: ApiException) {
Log.w("TAG", "signInResult:failed code=" + e.getStatusCode());
}
}
请告诉我记录此内容时得到的结果。
如果您还可以上传您在SignIn Activity中编写的整个代码以及google-services.json文件以便我可以调试该问题,那就太棒了。
修改强> 添加此
if (resultCode == Activity.RESULT_OK)
在requestCode == RC_SIGN_IN
之后
答案 1 :(得分:0)
更好地检查google sample并像这样处理异常
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
这样,您就不会丢失sign in status codes并可以检查common status code。