I am creating an OTP verification app using Firebase Authentication with a phone number. I did all the stuff like add dependencies,pugin, JSON file, and connect Firebase Authentication but still have this error. please tell me what should i do.
public class MainActivity extends AppCompatActivity {
EditText editMobileNumber;
Button sendButton;
FirebaseAuth mAuth;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth=FirebaseAuth.getInstance();
editMobileNumber=findViewById(R.id.editMobileNumber);
sendButton=findViewById(R.id.getOTP);
sendButton.setOnClickListener(v -> {
String number=editMobileNumber.getText().toString().trim();
if(number.length() != 10){
Toast.makeText(this,"Enter a valid number",Toast.LENGTH_SHORT).show();
}
else{
otpSend(number);
}
}
);
}
private void otpSend(String number) {
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
Toast.makeText(MainActivity.this,number,Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken token) {
Toast.makeText(MainActivity.this,verificationId,Toast.LENGTH_SHORT).show();
Intent i=new Intent(MainActivity.this, verifyOTP.class);
i.putExtra("verificationId",verificationId);
startActivity(i);
finish();
}
};
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber("+91" + number) // Phone number for Otp
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(this)
.setCallbacks(mCallbacks) // OnVerificationStateChangedCallbacks
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
}
verification is on next activity. here is the code :-
public class verifyOTP extends AppCompatActivity {
PinView otpPinView;
Button resendOTPButton,verifyButton;
TextView textView;
String verificationId= getIntent().getStringExtra("verificationId"),otp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_otp);
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()) {
// If Sign in success
} else {
// If sign in failed
Toast.makeText(this,"Invalid OTP!!",Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
}
the logcat show error is:-
-
Failed to initialize reCAPTCHA config: No Recaptcha Enterprise siteKey configured for tenant/project *
-
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.vivetales/com.example.vivetales.verifyOTP}: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String android.content.Intent.getStringExtra(java.lang.String)’ on a null object reference
2
Answers
Check if phone authentication is enabled in the Firebase console
You’re getting this error:
Because of the following line of code:
By the time you’re calling
getIntent().
, there is no intent available at that point. This means thatgetIntent()
isnull
. So you cannot callgetIntent()
beforeonCreate()
is called. So to solve this, you have to move the declaration ofverificationId
insideonCreate()
.