-1

I am planning to make an application using Firebase Auth with Google. But when I select my google account from the Google button it displays the following error message (At the end of this post). Also, please notice that the startActivityForResult is Deprecated. I would really appreciate help on this, as I'm new to Kotlin.

Thank You

Xml Code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".adapters.Sign_in_page">


    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="match_parent"
        android:layout_height="60sp"
        android:layout_marginLeft="50sp"
        android:layout_marginTop="512dp"
        android:layout_marginRight="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.16"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin Code

class Sign_in_page : AppCompatActivity() {

    private lateinit var mauth : FirebaseAuth
    private lateinit var mGoogleSignInClient: GoogleSignInClient
    private val RC_SIGN_IN = 9001

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

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("972503126282-sv3aqkustdhtq49a605hnvauubroht95.apps.googleusercontent.com")
            .requestEmail()
            .build()

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso)

        mauth = FirebaseAuth.getInstance()
  sign_in_button.setOnClickListener {
            resultLauncher.launch(Intent(mGoogleSignInClient.signInIntent))
        }  var resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val intent: Intent? = result.data
            val task = GoogleSignIn.getSignedInAccountFromIntent(intent)
            try {

                val account = task.getResult(ApiException::class.java)!!
                firebaseAuthWithGoogle(account.idToken!!)

            } catch (e: ApiException) {
                Toast.makeText(this, "Sign In Failed!", Toast.LENGTH_SHORT).show()

            }
        }
    }
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                // Google Sign In was successful, authenticate with Firebase
                val account = task.getResult(ApiException::class.java)!!
                //Log.d("TAG", "firebaseAuthWithGoogle:" + account.id)
                firebaseAuthWithGoogle(account.idToken!!)
            } catch (e: ApiException) {
                // Google Sign In failed, update UI appropriately
               // Log.w("TAG", "Google sign in failed", e)
                // ...
            }
        }
    }



    private fun firebaseAuthWithGoogle(idToken: String) {
        val credential = GoogleAuthProvider.getCredential(idToken, null)
        mauth.signInWithCredential(credential)
                .addOnCompleteListener(this) { task ->
                    if (task.isSuccessful) {
                        // Sign in success, update UI with the signed-in user's information
                      //  Log.d("TAG", "signInWithCredential:success")
                        val user = mauth.currentUser
                      //  updateUI(user)
                    } else {
                        // If sign in fails, display a message to the user.
                       // Log.w("TAG", "signInWithCredential:failure", task.exception)
                        // ...
                        Toast.makeText(this, "Authentication Failed.", Toast.LENGTH_SHORT).show()
                       // updateUI(null)
                    }

                    // ...
                }
    }
}

Error

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
W/System.err:     at android.view.ViewRootImpl.getHostVisibility(ViewRootImpl.java:1806)
        at android.view.ViewRootImpl.handleAppVisibility(ViewRootImpl.java:1442)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4838)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7078)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
CoDeR77
  • 11
  • 1
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jeroen Steenbeeke Jun 11 '21 at 11:55

1 Answers1

1

It was an issue with SHA1 fingerprint....got solved after i deleted the Oauth Client created in Google Cloud Platform.....and then updated in Firebase with same SHA1 fingerprint .....thus it worked!

CoDeR77
  • 11
  • 1