Firebase Phone Auth发送验证码时应用崩溃问题排查
问题:Firebase手机号验证调用sendVerificationCode时应用崩溃
在使用Firebase Phone Auth实现手机号验证流程时,调用sendVerificationCode方法及onCreate环节应用崩溃。Logcat显示错误源于com.google.android.gms.common.internal.Preconditions.checkNotEmpty,触发点是PhoneAuthProvider.verifyPhoneNumber调用。业务流程为用户邮箱密码注册后跳转至VerifyPhone Activity完成手机号验证,再进入MainActivity。目前调用sendVerificationCode后能成功发送OTP,但应用同时崩溃。
Logcat错误信息
at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(com.google.android.gms:play-services-basement@@17.6.0:2) at com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(Unknown Source:7) at com.studyhours.studystudios.activities.VerifyPhone.sendVerificationCode(VerifyPhone.java:144) at com.studyhours.studystudios.activities.VerifyPhone.onCreate(VerifyPhone.java:137) at android.app.Activity.performCreate(Activity.java:8151) at android.app.Activity.performCreate(Activity.java:8135)
相关代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_verify_phone); mAuth = FirebaseAuth.getInstance(); // Passing and Storing the phone number from the user activity final String phonenumber = getIntent().getStringExtra("phonenumber"); final TextView otpPhone = findViewById(R.id.otpPhone); resendOtp = (TextView) findViewById(R.id.resendBtn); // getting email and phone from Register activity through intent final String getPhone = getIntent().getStringExtra("phone"); // setting email and phone to TextView otpPhone.setText(getPhone); findViewById(R.id.verifyBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String code = editText1.getText().toString() + editText2.getText().toString() + editText3.getText().toString() + editText4.getText().toString() + editText5.getText().toString() + editText6.getText().toString(); if (code.isEmpty()) { Toast.makeText(VerifyPhone.this, "Please enter the code", Toast.LENGTH_SHORT).show(); return; } verifyCode(code); } }); resendOtp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // handling resend code here resendOtp(phonenumber); } }); // calling send OTP sendVerificationCode(phonenumber); } public void sendVerificationCode(String number) { progressBar.setVisibility(View.GONE); PhoneAuthProvider.getInstance().verifyPhoneNumber( number, 60, TimeUnit.SECONDS, this, mCallBack ); }
解决方案
崩溃的核心原因是传递给PhoneAuthProvider.verifyPhoneNumber的手机号参数为空值,触发了Google服务的非空检查。
1. 统一Intent参数键名
代码中同时使用"phonenumber"和"phone"两个不同键名获取手机号,大概率是Register页面传递数据时只使用了其中一个键名,导致另一个键获取的值为null。
检查Register页面传递手机号的代码,比如如果是:
intent.putExtra("phone", userPhone);
那VerifyPhone页面要统一使用"phone"获取:
final String phonenumber = getIntent().getStringExtra("phone");
确保传递和接收的键名完全一致。
2. 添加非空校验
在调用sendVerificationCode前,先检查手机号是否为空,避免传递null给Firebase方法:
// 替换原有的sendVerificationCode(phonenumber); if (phonenumber != null && !phonenumber.trim().isEmpty()) { sendVerificationCode(phonenumber); } else { Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show(); finish(); // 可根据业务逻辑处理,比如返回上一页 }
3. 确认手机号格式
额外注意:Firebase要求手机号必须包含国家代码前缀(如+86),如果格式不正确也可能引发异常,建议在传递前确保格式合规。
内容的提问来源于stack exchange,提问作者Aditya
相关产品推荐
相关产品推荐

