1

Hello I am getting this exception continuously on launch of app :

2019-02-18 16:33:14.735 2080-2080/? E/AndroidRuntime: FATAL EXCEPTION: main Process: assus.oumayma.com.firebasekotlinapp, PID: 2080 java.lang.RuntimeException: Unable to start activity ComponentInfo{assus.oumayma.com.firebasekotlinapp/assus.oumayma.com.firebasekotlinapp.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process assus.oumayma.com.firebasekotlinapp. Make sure to call FirebaseApp.initializeApp(Context) first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2725) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2786) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1501) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:173) at android.app.ActivityThread.main(ActivityThread.java:6459) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828) Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process assus.oumayma.com.firebasekotlinapp. Make sure to call FirebaseApp.initializeApp(Context) first. at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240) at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source) at assus.oumayma.com.firebasekotlinapp.MainActivity.onCreate(MainActivity.kt:23) at android.app.Activity.performCreate(Activity.java:6673) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)

and this the code:

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()
    }

the build.gradle:

implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
1
  • Please add the entire content of your both build.gradle files. Commented Feb 18, 2019 at 16:18

1 Answer 1

1

You are trying to get an instance of Firebase without initialise it. Please add this line of code before you try to get an instance of Firebase:

FirebaseApp.initializeApp(this);

If you are using google service 4.1.0

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

then update the version to

classpath 'com.google.gms:google-services:4.2.0'
Sign up to request clarification or add additional context in comments.

6 Comments

How to do in fragment?
Just add this on your Activity onCreate method then no need to add in Fragment.
Which version of google service you have currently used?
then upgrade to classpath 'com.google.gms:google-services:4.2.0' and try again.
thanks it's run when I change classpath 'com.google.gms:google-services:4.2.0'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.