我想获得当用户无法使用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();
我认为这可以通过开关盒实现,但我不知道该怎么做。
这是我应该使用的:
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();
}
}
}`