ava.lang.IllegalStateException:默认FirebaseApp未初始化

时间:2019-02-18 15:57:04

标签: android firebase

您好,我在启动应用程序时不断遇到此异常:

  

2019-02-18 16:33:14.735 2080-2080 /? E / AndroidRuntime:致命异常:main       流程:assus.oumayma.com.firebasekotlinapp,PID:2080       java.lang.RuntimeException:无法启动活动ComponentInfo {assus.oumayma.com.firebasekotlinapp / assus.oumayma.com.firebasekotlinapp.MainActivity}:java.lang.IllegalStateException:默认FirebaseApp在此过程中未初始化为assus.oumayma.com .firebasekotlinapp。确保首先调用FirebaseApp.initializeApp(Context)。           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2725)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2786)           在android.app.ActivityThread.-wrap12(ActivityThread.java)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1501)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:173)           在android.app.ActivityThread.main(ActivityThread.java:6459)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:938)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)        由以下原因引起:java.lang.IllegalStateException:在此过程中,assus.oumayma.com.firebasekotlinapp没有初始化默认的FirebaseApp。确保首先调用FirebaseApp.initializeApp(Context)。           com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common @@ 16.0.2:240)           位于com.google.firebase.auth.FirebaseAuth.getInstance(未知来源)           在assus.oumayma.com.firebasekotlinapp.MainActivity.onCreate(MainActivity.kt:23)           在android.app.Activity.performCreate(Activity.java:6673)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)

这是代码:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mAuth = FirebaseAuth.getInstance()
        signOut.setOnClickListener {
                view: View? -> mAuth.signOut()
            startActivity(Intent(this, PhoneAuthenfication::class.java))
            Toast.makeText(this, "Logged out Successfully :)", Toast.LENGTH_LONG).show()
        }
    }


    override fun onStart() {
        super.onStart()
        if (mAuth.currentUser == null) {
            startActivity(Intent(this, PhoneAuthenfication::class.java))
        }else {
            Toast.makeText(this, "Already Signed in :)", Toast.LENGTH_LONG).show()
        } 
    }

}



class PhoneAuthenfication : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_phone_authenfication)

        mAuth = FirebaseAuth.getInstance()
        veriBtn.setOnClickListener { view: View? ->
            progress.visibility = View.VISIBLE
            verify()
        }
        authBtn.setOnClickListener { view: View? ->
            progress.visibility = View.VISIBLE
            authenticate()
        }
    }


    private fun verificationCallbacks() {
        mCallbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            override fun onVerificationCompleted(credential: PhoneAuthCredential) {
                progress.visibility = View.INVISIBLE
                signIn(credential)
            }

            override fun onVerificationFailed(p0: FirebaseException?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onCodeSent(verfication: String?, p1: PhoneAuthProvider.ForceResendingToken?) {
                super.onCodeSent(verfication, p1)
                verificationId = verfication.toString()
                progress.visibility = View.INVISIBLE
            }

        }
    }

    private fun verify() {

        verificationCallbacks()

        val phnNo = phnNoTxt.text.toString()

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phnNo,
            60,
            TimeUnit.SECONDS,
            this,
            mCallbacks
        )
    }

    private fun signIn(credential: PhoneAuthCredential) {
        mAuth.signInWithCredential(credential)
            .addOnCompleteListener { task: Task<AuthResult> ->
                if (task.isSuccessful) {
                    toast("Logged in Successfully :)")
                    startActivity(Intent(this, MainActivity::class.java))
                }
            }
    }

    private fun authenticate() {

        val verifiNo = verifiTxt.text.toString()

        val credential: PhoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, verifiNo)

        signIn(credential)

    }

    private fun toast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }

build.gradle

implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'

1 个答案:

答案 0 :(得分:1)

您正尝试获取Firebase实例而不对其进行初始化。在尝试获取Firebase实例之前,请添加以下代码行:

FirebaseApp.initializeApp(this);

如果您使用的是Google服务4.1.0

classpath 'com.google.gms:google-services:4.1.0'

然后将版本更新为

classpath 'com.google.gms:google-services:4.2.0'