You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase手机号OTP验证遇空指针及reCAPTCHA配置错误求助

解决Firebase手机号OTP验证的两个错误

错误1:Failed to initialize reCAPTCHA config: No Recaptcha Enterprise siteKey configured

解决步骤:

    1. 启用Firebase Phone登录:进入Firebase控制台 → 目标项目 → Authentication → 登录方式 → 找到「Phone」选项,点击启用,按提示完成配置(系统会自动生成reCAPTCHA密钥)。
    1. 更新google-services.json:从Firebase控制台重新下载最新的google-services.json文件,替换app模块根目录下的旧文件,确保文件包含reCAPTCHA相关配置。
    1. 检查依赖配置:确保app/build.gradle中包含Firebase Auth的最新依赖,示例如下:
// 方式1:单独指定版本
implementation 'com.google.firebase:firebase-auth:22.3.1'

// 方式2:使用Firebase BoM统一管理版本(推荐)
implementation platform('com.google.firebase:firebase-bom:32.7.0')
implementation 'com.google.firebase:firebase-auth'
    1. 确认权限:AndroidManifest.xml中添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />

错误2:NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(...)' on a null object reference

问题原因:

verifyOTP类中,成员变量verificationId在类初始化阶段调用getIntent(),此时Activity尚未完成初始化,Intent对象为空,触发空指针异常。

修复代码:

将verificationId的初始化移到onCreate()方法内部(super.onCreate(savedInstanceState)之后执行):

public class verifyOTP extends AppCompatActivity {

    PinView otpPinView;
    Button resendOTPButton,verifyButton;
    TextView textView;
    String verificationId, otp; // 仅声明变量,不提前初始化

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_otp);

        // 在此处初始化verificationId
        verificationId = getIntent().getStringExtra("verificationId");

        otpPinView=findViewById(R.id.otppinview);
        resendOTPButton=findViewById(R.id.resendOTPButton);
        verifyButton=findViewById(R.id.verifyButton);
        textView=findViewById(R.id.verifyOTP);

        textView.setOnClickListener(v -> {
            Toast.makeText(this,"it works",Toast.LENGTH_SHORT).show();
        });

        verifyButton.setOnClickListener(v ->{
            otp= Objects.requireNonNull(otpPinView.getText()).toString();

            if(otp.length() != 6){
                Toast.makeText(this,"Invalid OTP!!",Toast.LENGTH_SHORT).show();
            }
            else{
                if(verificationId != null){
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, otp);

                    FirebaseAuth.getInstance().signInWithCredential(credential)
                            .addOnCompleteListener(this, task -> {
                                if (task.isSuccessful()) {
                                    // 登录成功后的逻辑,例如跳转主页
                                } else {
                                    Toast.makeText(this,"Invalid OTP!!",Toast.LENGTH_SHORT).show();
                                }
                            });
                }
            }
        });
    }
}

额外调试建议:

修改MainActivity中onVerificationFailed方法,显示具体错误信息而非手机号,便于排查问题:

@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
    Toast.makeText(MainActivity.this, "验证失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}

内容的提问来源于stack exchange,提问作者Abhay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:41:01