Android应用程序在Firebase电话身份验证时崩溃 [英] Android app crashes on firebase phone authentication

查看:42
本文介绍了Android应用程序在Firebase电话身份验证时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase进行电话号码身份验证.使用电话号码时,它会自动进行验证.但是,当我使用另一个电话号码时,我会收到 classCastException .它显示"getGoogleApiForMethod()返回的Gms"..stackoverflow链接.答案是在电话或仿真器中降级Firebase或Google Play服务.我在下面也包括gradle.请检查

I am using firebase for phone number authentication.When I used my phone number, it automatically verifies it.But when I use another phone number I get classCastException.It says "getGoogleApiForMethod() returned Gms".There is a similar problem in stackoverflow Link.The answer was to downgrade firebase or google play services in phone or emulator.I have included gradle as well below.Please check it

例外:

W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@c2c24f7
05-11 15:00:05.597 29282-29282/ W/System.err: java.lang.ClassCastException: Otp.OtpCodeFrag cannot be cast to java.util.concurrent.Executor
05-11 15:00:05.607 29282-29282/ W/System.err:     at Otp.OtpCodeFrag.signInWithPhoneAuthCredential(OtpCodeFrag.java:402)
        at Otp.OtpCodeFrag.access$200(OtpCodeFrag.java:39)
        Otp.OtpCodeFrag$2.onClick(OtpCodeFrag.java:222)
        at android.view.View.performClick(View.java:5716)
        at android.widget.TextView.performClick(TextView.java:10926)
        at android.view.View$PerformClick.run(View.java:22596)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

OTPFrag.java:

OTPFrag.java:

        onVerificationStateChangedCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks()
                    {
                        @Override
                        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential)
                        {
                            phoneauthCredential = phoneAuthCredential;
                            automatic_verification = true;

                            Toast.makeText(getContext(), "Verification Completed", Toast.LENGTH_SHORT).show();

                        }
                        @Override
                        public void onVerificationFailed(FirebaseException e) {
                            Toast.makeText(getContext(),"Verification Failed",Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                        @Override
                        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken)
                        {
                            super.onCodeSent(s, forceResendingToken);
                            Toast.makeText(getContext(),"CodeSent",Toast.LENGTH_SHORT).show();
                            verificationid=s;
                            codesent=true;
                            token=forceResendingToken.toString();
                            Log.e("Token",token);
                            Log.e("ID",s);
        //                           smscode=phoneauthCredential.getSmsCode();
                        }
                        };

                    PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        phonenumber,        // Phone number to verify
                        4,                 // Timeout duration
                        TimeUnit.SECONDS,   // Unit of timeout
                        getActivity(),               // Activity (for callback binding)
                        onVerificationStateChangedCallbacks);

    otpCodeLytBinding.verifyOtp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {

                code1=otpCodeLytBinding.cd1.getText().toString();
                code2=otpCodeLytBinding.cd2.getText().toString();
                code3=otpCodeLytBinding.cd3.getText().toString();
                code4=otpCodeLytBinding.cd4.getText().toString();
                code5=otpCodeLytBinding.cd5.getText().toString();
                code6=otpCodeLytBinding.cd6.getText().toString();
                   if (code1!=null&&!code1.equals("")&&code2!=null&&
                   !code2.equals("")&&code3!=null&&!code3.equals("")
                   &&code4!=null&&!code4.equals("")&&code5!=null
                   &&!code5.equals("")&&code6!=null&&!code6.equals(""))
               {
                   completecode=code1+code2+code3+code4+code5+code6;
                   Log.e("cm",completecode);
                   try {
                       if (verificationid!=null&&completecode!=null){

                           PhoneAuthCredential credential = PhoneAuthProvider.
                               getCredential(verificationid, completecode);
                           signInWithPhoneAuthCredential(credential);
                       }
                       else {
                           Toast.makeText(getContext(),"Something null",Toast.LENGTH_SHORT).show();
                       }
                   }
                   catch (Exception e)
                   {
                       e.printStackTrace();
                   }

               }
               else {
                       Toast.makeText(getContext(),"Please enter otp",Toast.LENGTH_SHORT).show();
                   }
            }
        });
     private void signInWithPhoneAuthCredential(PhoneAuthCredential credential)
        {
            Log.e("SigningIn","Started");
            mAuth.signInWithCredential(credential)
                    .addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
    //                            Log.d(TAG, "signInWithCredential:success");
    //
    //                            FirebaseUser user = task.getResult().getUser();
                                                          }
 else {
                                // Sign in failed, display a message and update the UI
                                Log.w(TAG, "signInWithCredential:failure", task.getException());
                                if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                    // The verification code entered was invalid
                                }
                            }
                        }
                    });
        }

build.gradle(app):

build.gradle(app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.root5solutions.loc8official"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.balysv:material-ripple:1.0.2'

//    implementation 'com.appolica:flubber:1.0.1'
    implementation 'com.google.firebase:firebase-core:15.0.2'
    implementation 'com.google.firebase:firebase-auth:15.1.0'
    testImplementation 'junit:junit:4.12'
//    androidTestImplementation 'com.android.support.test:runner:1.0.2'
//    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

build.gradle(项目):

build.gradle(Project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'com.google.gms:google-services:3.2.0'



        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

推荐答案

我不知道您要使用此行做什么:

I don't know what you're trying to do with this line:

.addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {

this 投射到 Executor 似乎没有任何意义.您想要的而不是 this 的内容是对当前Activity的引用.尝试使用 getActivity()来获取此参考.

Casting this to Executor doesn't seem to make sense. What you want instead of this is a reference to the current Activity. Try getActivity() instead to get this reference.

这篇关于Android应用程序在Firebase电话身份验证时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆