Firebase电话身份验证Android崩溃

时间:2018-09-15 17:54:08

标签: java android firebase firebase-authentication

所以我正在使用Firebase数据库和Auth,我正在尝试构建Phone Auth,但是由于某种原因,该应用程序崩溃了..不知道为什么。 我是刚开始使用Android Studio和Firebase数据库上的第一次编程进行编程的人,不知道为什么应用程序会崩溃,但是在按下激活verifySignInCode()的按钮后它崩溃了,它确实会发送一封代码为a的电子邮件 日志猫错误在这里

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)

这是MainActivity

    /// 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”后,崩溃发生了

2 个答案:

答案 0 :(得分:0)

应用崩溃,因为方法credential无效,phoneAuth无法正常工作。可以通过在try方法上使用catchsignInWithPhoneAuthCredential()来解决此问题。

在代码中它将如下所示:

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值。

答案 1 :(得分:0)

只需在调用phoneAuth函数之前定义String代码。

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

            signInWithPhoneAuthCredential(credential);>