1

I am trying to do registration using firebase OTP verification,

I have two EditText fields on my registration page

(1) Enter Name

(2)Enter Phone Number

After getting Phone no. and Name and I am trying to get OTP using Firebase.

and it is working on every device but not working on the Nexus 5.

Error that I am getting

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task. com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)' on a null object reference

While executing PhoneAuthProvider.getInstance().verifyPhoneNumber

Here is My code

FirebaseApp.initializeApp(this);
    mAuth = FirebaseAuth.getInstance();
    CallBackInitialize();

private void CallBackInitialize(){
    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {

            mVerificationInProgress = false;
            updateUI(STATE_VERIFY_SUCCESS, credential);

        }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        // This callback is invoked in an invalid request for verification is made,
        // for instance if the the phone number format is not valid.
        Log.w(TAG, "onVerificationFailed", e);
        // [START_EXCLUDE silent]
        mVerificationInProgress = false;
        // [END_EXCLUDE]
        if (e instanceof FirebaseAuthInvalidCredentialsException) {
            // Invalid request
            //mPhoneNumberField.setError("Invalid phone number.");
        } else if (e instanceof FirebaseTooManyRequestsException) {
            // The SMS quota for the project has been exceeded
            Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",Snackbar.LENGTH_SHORT).show();
        }
        // Show a message and update the UI
        updateUI(STATE_VERIFY_FAILED);
    }

    @Override
    public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) {
        // The SMS verification code has been sent to the provided phone number, we
        // now need to ask the user to enter the code and then construct a credential
        // by combining the code with a verification ID.
        Log.d(TAG, "onCodeSent:" + verificationId);

        mVerificationId=verificationId;

        Intent i = new Intent(RegistrationActivity.this, SignUpVerify.class);
        i.putExtra("mobile", mobilenumber.getText().toString());
        //i.putExtra("otp1", otp);
        i.putExtra("vCode", mVerificationId);
        i.putExtra("name", user_name.getText().toString());
        startActivity(i);
        overridePendingTransition(R.anim.righttoleft, R.anim.slide_out_left);

        progressBar.setVisibility(View.GONE);

        signup.setClickable(true);

        // Save verification ID and resending token so we can use them later
        // mVerificationId = verificationId;
        //mResendToken = token;
        // Update UI
        updateUI(STATE_CODE_SENT);
    }
};}

After getting Phone no.

    private void sendSmsToGivenMobileNumber(String phonrNumber){
            Log.d("Phone",phonrNumber);
            FirebaseAuth.getInstance().signOut();
            startPhoneNumberVerification(phonrNumber);
        }

 private void startPhoneNumberVerification(String phoneNumber) {
        try {
            mAuth = FirebaseAuth.getInstance();
            PhoneAuthProvider provider = PhoneAuthProvider.getInstance(mAuth);
            provider.verifyPhoneNumber(
                    "+91" + phoneNumber,        // Phone number to verify
                    60,                 // Timeout duration
                    TimeUnit.SECONDS,   // Unit of timeout
                    RegistrationActivity.this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
            mVerificationInProgress = true;

        }catch (Exception ex) {
            System.out.println("Closed : " + ex.getMessage().toString());
        }

    }
Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55

2 Answers2

0

I have got the solution .

Problem not in code .Just problem in Google Play Services

Solution 1 :

Just update your device Google Play Service its .

or

Solution 2 :

Check your gmail_id is added in device .It should be addedd

0

I had been facing the same trouble. There is a little bit of error i.e. in the placement of the code. The only thing you have to do in this code is to place the code of "mCallbacks" above the function call [ startPhoneNumberVerification() ] in onCreate().

This will run your code.