使用不同帐户的 Google 登录在应用程序中崩溃

Posted

技术标签:

【中文标题】使用不同帐户的 Google 登录在应用程序中崩溃【英文标题】:Google Sign in from different account crashes in the app 【发布时间】:2019-10-19 15:45:57 【问题描述】:

我已将谷歌登录集成到我的应用中。我点击按钮并选择“从其他帐户登录”,让用户像往常一样登录谷歌登录,然后它崩溃了。

我已经关注了官方文档:https://developers.google.com/identity/sign-in/android/sign-in

 private void googleLogin() 
    Intent intent = googleSignInClient.getSignInIntent();
    startActivityForResult(intent, GOOGLE_KEY_CODE);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    callbackManager.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GOOGLE_KEY_CODE) 
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try 
            GoogleSignInAccount account = task.getResult(ApiException.class);
            assert account != null;
            String google_email = account.getEmail();
            String google_name = account.getDisplayName();
            String[] fullname = Objects.requireNonNull(google_name).split(" ");
            String firstname = fullname[0];
            String lastname = fullname[1];

            if (google_email != null) 
                loginFromGmail(google_email, firstname, lastname);
                Log.d("google_email", google_email);
                Log.d("google_email", google_name);
            
         catch (ApiException e) 
            e.printStackTrace();
        
    

错误日志是-->

 java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1, result=-1, data=Intent  (has extras)  to activity com.wars/com.wars.activities.RegisterActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4382)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4426)
    at android.app.ActivityThread.-wrap20(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1685)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6626)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
    at com.wars.activities.RegisterActivity.onActivityResult(RegisterActivity.java:403)
    at android.app.Activity.dispatchActivityResult(Activity.java:7305)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4378)

【问题讨论】:

立即更新您的 google 应用,例如 google、启动器等 我确实更新了,谢谢。现在至少我得到了一些错误。它说'未能提供结果'@Quicklearner 很高兴知道这里有错误的代码' 并确保您的应用也在其他设备上 更新了代码。请检查。它在 onActivityResult @Quicklearner 中给出错误 【参考方案1】:

此行导致错误

String[] fullname = Objects.requireNonNull(google_name).split(" ");

如果您想获取全名,然后将其拆分为名字和姓氏,请执行此操作

String first_name ="",last_name="";
 String fullname = account.getDisplayName();
            try 
                if (fullname != null) 
                    if (!fullname.equalsIgnoreCase("")) 
                        String[] name_array = fullname.split(" ");
                        if (name_array.length > 0) 
                            first_name = name_array[0];
                            last_name = name_array[1];
                        

                    
                 else 
                    // do stuff
                

             catch (Exception e) 
                e.printStackTrace();
            

完整代码sn-p

String firstname="",lastname="";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    callbackManager.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GOOGLE_KEY_CODE) 
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try 
            GoogleSignInAccount account = task.getResult(ApiException.class);
            assert account != null;
            String google_email = account.getEmail();
            String google_name = account.getDisplayName();
           try 
                    if (google_name != null) 
                        if (!google_name.equalsIgnoreCase("")) 
                            String[] name_array = google_name.split(" ");
                            if (name_array.length > 0) 
                                firstname= name_array[0];
                                lastname = name_array[1];
                            

                        
                     else 
                        // handle the null case in case user does not have display name in gmail account
                google_name = "";
                firstname= "";
                lastname = "";
                    

                 catch (Exception e) 
                    e.printStackTrace();
                

            if (google_email != null) 
                loginFromGmail(google_email, firstname, lastname);
                Log.d("google_email", google_email);
                Log.d("google_email", google_name);
            
         catch (ApiException e) 
            e.printStackTrace();
        
    

【讨论】:

好的,很高兴知道,只需支持我的答案,以便其他人也可以参考 没有足够的声望来点赞,但打勾图标。【参考方案2】:

问题在于字里行间:

  String[] fullname = Objects.requireNonNull(google_name).split(" ");
            String firstname = fullname[0];
            String lastname = fullname[1];

您收到空指针异常,因为 fullname[1] 处没有任何内容,这向我表明您的行:

  String[] fullname = Objects.requireNonNull(google_name).split(" ");

未正确拆分姓名对姓氏和名字执行 Log.d(),您要么未正确拆分姓名,要么名字将包含完整的名字和姓氏。

【讨论】:

以上是关于使用不同帐户的 Google 登录在应用程序中崩溃的主要内容,如果未能解决你的问题,请参考以下文章

每次用户登录时,Google 登录都使用相同的帐户

如何在JMeter中使用Google帐户登录

在 SwiftUI 中使用 google 帐户登录后是不是有任何改变视图的方法?

如何在使用 Google 登录按钮时不将 Google 帐户添加到 Android 手机

通过google账号登录iOS应用并返回应用

Google Plus 登录帐户选择对话框问题