Android Google登录例外

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Google登录例外相关的知识,希望对你有一定的参考价值。

我想获得当用户无法使用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();
        }
    }
}`

以上是关于Android Google登录例外的主要内容,如果未能解决你的问题,请参考以下文章

从 Android 登录时,Google OAuth2 返回错误 10 或错误 12500

google登录android后报错12501

RecyclerView holder中的Android Google Maps动态片段

片段中的 Xamarin Android Google 地图错误

android google map supportmap片段无法在片段中初始化

Android:如何在Google Play开发者控制台中获取有关已捕获例外的信息?