我已经将谷歌登录集成到我的应用程序中。我单击按钮并选择"从其他帐户登录",它让用户像往常一样登录谷歌登录,然后它崩溃了。
我遵循了官方文档:
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)
此行导致错误
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();
}
}
}
问题出在字里行间:
String[] fullname = Objects.requireNonNull(google_name).split(" ");
String firstname = fullname[0];
String lastname = fullname[1];
您收到一个空指针异常,因为全名 [1] 中没有任何内容向我表明您的行:
String[] fullname = Objects.requireNonNull(google_name).split(" ");
没有正确拆分名称,对姓氏和名字执行 Log.d((,您要么没有正确拆分名称,要么名字将包含完整的名字和姓氏。