我正在尝试使用firebase OTP验证进行注册,
我的注册页面上有两个EditText字段
(1)输入姓名
(2)输入电话号码
获得电话号码后和姓名,我正在尝试使用Firebase获取OTP。
它正在处理所有设备,但没有使用Nexus 5.
我收到错误
java.lang.NullPointerException:尝试调用虚拟方法'com.google.android.gms.tasks.Task。有关空对象引用的com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)
执行PhoneAuthProvider.getInstance().verifyPhoneNumber
这是我的代码
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);
}
};}
获取电话号码
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());
}
}
答案 0 :(得分:0)
我有解决方案。
问题不在代码中。只是Google Play服务中的问题
解决方案1:
只需将您的设备更新为Google Play服务。
或
解决方案2:
检查您的gmail_id已添加到设备中。应添加
答案 1 :(得分:-1)
我一直面临同样的麻烦。存在一点点错误,即在代码的放置中。 您在此代码中唯一要做的就是将“mCallbacks”的代码置于onCreate()函数调用[startPhoneNumberVerification()]之上。
这将运行您的代码。
干杯!!