当用户无法使用谷歌登录登录到我的应用程序时,我想得到异常。例如,在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();我认为这可以通过开关实现,但我不知道如何做到这一点。
发布于 2018-12-13 17:51:18
这是我应该使用的:
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();
}
}
}`https://stackoverflow.com/questions/53767262
复制相似问题