简体   繁体   English

应用中其他帐户的Google登录失败

[英]Google Sign in from different account crashes in the app

I have integrated google sign-in into my app. 我已将google登录功能集成到我的应用中。 I click on the button and choose 'Sign in from different account' it let's the user sign in like usual google sign in and then it crashes. 我单击按钮,然后选择“从其他帐户登录”,让用户像通常的google登录一样登录,然后崩溃。

I have followed the official documentation: 我遵循了官方文档:
https://developers.google.com/identity/sign-in/android/sign-in 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();
        }
    }
}

and the error log is --> 错误日志是->

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=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)

This line causes the error 该行导致错误

String[] fullname = Objects.requireNonNull(google_name).split(" "); String []全名= Objects.requireNonNull(google_name).split(“”);

If you want to get full name and then split it for first name and last name do this 如果要获取全名,然后将其拆分为名字和姓氏,请执行此操作

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();
            }

Complete code snippet 完整的代码段

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();
        }
    }
}

The issue is between the lines: 问题出在两行之间:

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

You're getting a nullpointer exception because there is nothing at fullname[1] which suggests to me that your line: 您会收到一个nullpointer异常,因为全名[1]上没有任何内容向我提示您的行:

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

is not correctly splitting the names do a Log.d() on the lastname and firstname, you're either not splitting the names correctly or the firstname will incorporate the full first and last name. 未正确拆分名称,请对姓氏和名字进行Log.d(),或者您未正确拆分名称,否则姓氏将包含完整的名字和姓氏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM