我想获取当用户无法使用 Google 登录登录我的应用程序时导致的异常。例如,在 handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask)
中我使用的方法:
private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
try {
account = completedTask.getResult(ApiException.class);
if(isSignedIn()) {
Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Home.class));
}
} catch (ApiException e) {
Toast.make(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();
}
}
}
这有助于我找出错误的原因。但最常见的原因是当没有选择帐户时,此代码返回 null
.
但我想做一个 Toast
当没有选择帐户时:
Toast.makeText(this, "Failed to login because: No account selected!", Toast.LENGTH_LONG).show();
我认为这可以使用 switch case 来实现,但我不知道该怎么做。
最佳答案
这是我应该使用的:
private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
try {
account = completedTask.getResult(ApiException.class);
if(isSignedIn()) {
Toasty.success(this, "Success!", Toast.LENGTH_SHORT, true).show();
startActivity(new Intent(MainActivity.this, Home.class));
}
} catch (ApiException e) {
if(e.getCause() != null) {
Toast.makeText(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Failed to login because: No account Selected!", Toast.LENGTH_LONG).show();
}
}
}`
关于java - Android Google 登录异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767262/