火碱手机身份验证安卓崩溃



所以我正在使用Firebase数据库和身份验证,我正在尝试构建Phone Auth,但由于某种原因该应用程序崩溃..不知道为什么.. 我是使用 android 工作室和 Firebase 数据库的第一个计时器编程的新手,不确定为什么该应用程序崩溃,但在我按下激活 verifySignInCode(( insta 崩溃的按钮后它崩溃了,它确实发送了一封带有代码的电子邮件 日志猫错误在这里

09-15 20:39:41.804 22046-22046/com.example.erelpc.calltest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.erelpc.calltest, PID: 22046
java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, ortemprary proof.
at com.google.android.gms.common.internal.Preconditions.checkArgument(Unknown Source:8)
at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source:6)
at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source:33)
at com.example.erelpc.calltest.MainActivity.verifySignInCode(MainActivity.java:92)
at com.example.erelpc.calltest.MainActivity.access$100(MainActivity.java:28)
at com.example.erelpc.calltest.MainActivity$2.onClick(MainActivity.java:61)
at android.view.View.performClick(View.java:6877)

这是主要活动

/// SMS Handler
private void sendVerificationCode(){
String phone = etphonenumber.getText().toString();
if (phone.isEmpty()) {
etphonenumber.setError("Enter a Phone Number!");
etphonenumber.requestFocus();
return;
}
if (phone.length() != 9){
etphonenumber.setError("Please Enter a valid Number!");
etphonenumber.requestFocus();
return;
}
phone = "+972" + phone;
Intent intent =  new Intent(this, loginsuccess.class);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phone,        // Phone number to verify
60,                 // Timeout duration
TimeUnit.SECONDS,   // Unit of timeout
this,               // Activity (for callback binding)
mCallbacks);        // OnVerificationStateChangedCallbacks
}
private void verifySignInCode(){
String code = etcodeveri.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, code);
signInWithPhoneAuthCredential(credential);
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codesent = s;
}
};

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Login Success!", Toast.LENGTH_SHORT).show();
registerModule();
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(getApplicationContext(), "Login Unsuccess!", Toast.LENGTH_SHORT).show();
}
}
}
});
}  
public void registerModule(){
Intent intent =  new Intent(this, loginsuccess.class);
startActivity(intent);
}

我按下"btnAdd"后崩溃

应用程序崩溃,因为该方法未获得有效credential以使phoneAuth正常工作。可以通过对signInWithPhoneAuthCredential()方法使用trycatch来解决此问题。

在代码中,它将如下所示:

private void verifyCode(){
String code = cd.getText().toString(); // this is OTP
String pH = phone.getText().toString(); // this is phone number
if(code.equals("") && pH.equals(""))
Toast.makeText(MainActivity.this,"Nothing to validate", Toast.LENGTH_SHORT).show();
else {
try {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(otp, code);
signInWithPhoneAuthCredential(credential);
}
catch (Exception e) {
Log.i("exception",e.toString());
Toast.makeText(MainActivity.this,"Invalid credentials",Toast.LENGTH_LONG).show();
}
}
}

此外,您应该放置更多catchif语句,以避免在方法中赋予null值。

只需要在调用 phoneAuth 函数之前定义字符串代码。

< String code = phoneAuthCredential.getSmsCode();
assert code != null;
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);
signInWithPhoneAuthCredential(credential);>

相关内容

最新更新