【问题标题】:Firebase Google Sign in check if User phone Number is emptyFirebase Google 登录检查用户电话号码是否为空
【发布时间】:2024-05-23 02:05:01
【问题描述】:

我正在为我的 android 应用程序使用 google 登录,我想检查我当前用户的电话号码是否为空,然后采取行动。我尝试放置一个 if 语句,但它正在向我抛出空指针异常。有人可以帮我解决这个问题吗?

 String userNumber = user.getPhoneNumber();
  mProgressDialog.setMessage("Verifying your phone number information...");
      if(userNumber.isEmpty()) {
          Intent phoneAuth = new Intent(GoogleSignIn.this, 
                                          PhoneAuthentication.class);
          startActivity(phoneAuth);
          finish();
      }

这是错误:

08-03 19:09:24.680 29877-29877/com.manali.phonegooglecontacts D/FirebaseApp: Notified 0 auth state listeners.
08-03 19:09:24.683 29877-29877/com.manali.phonegooglecontacts D/AndroidRuntime: Shutting down VM
08-03 19:09:24.684 29877-29877/com.manali.phonegooglecontacts E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.manali.phonegooglecontacts, PID: 29877
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                    at com.manali.phonegooglecontacts.GoogleSignIn$1.onComplete(GoogleSignIn.java:132)
                                                                                    at com.google.android.gms.tasks.zzf.run(Unknown Source)
                                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

【问题讨论】:

  • 也许你应该在 'userNumber.isEmpty()' 之前执行这个 'null != userNumber' 。或使用 TextUtil 实用程序之一
  • 不要使用userNumber.isEmpty(),而是使用userNumber === null(如果为空)和userNumber != null(如果非空)
  • 电话号码为空时很难,我们必须手动进行身份验证,实际上登录后它应该提供用户电话号码,太难过了

标签: android firebase firebase-authentication google-signin


【解决方案1】:

实际上,您希望将电话号码作为唯一 ID,因此有时它为空,并且您不能将电子邮件用作 Firebase 的唯一 ID,因为它包含一些无法存储在 Firebase 中的字母,所以如果您想获取phone 只是因为存储为唯一 id 然后不要,只是获取

user.getid();

【讨论】: