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

时间:2019-06-04 06:21:06

标签: java android google-signin googlesigninaccount

我已将google登录集成到我的应用中。我单击按钮,然后选择“从其他帐户登录”,让用户像通常的google登录一样登录,然后崩溃。

我遵循了官方文档:
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 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)

2 个答案:

答案 0 :(得分:0)

此行会导致错误

  

String []全名= 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();
            }

完整的代码段

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

答案 1 :(得分:0)

问题出在两行之间:

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

您会收到一个null指针异常,因为全名[1]上没有任何内容向我提示您的行:

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

未正确拆分名称,对姓氏和名字进行Log.d(),您可能未正确拆分名称,或者姓氏将包含完整的名字和姓氏。