0

I had a java android app with androidx navigation (supportbar + navigation drawer) and it was launching Firebase UI to get the user logged in. Then I've decided to migrate the Activity code to Kotlin using the AS converter and some manual work on the navigation setup.

Despite my efforts it still breaks with this stack

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gabrielcalero.familytree, PID: 20260
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabrielcalero.familytree/com.firebase.ui.auth.KickoffActivity}: java.lang.NullPointerException: Attempt to invoke super method 'void androidx.fragment.app.FragmentActivity.onPanelClosed(int, android.view.Menu)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6524)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
 Caused by: java.lang.NullPointerException: Attempt to invoke super method 'void androidx.fragment.app.FragmentActivity.onPanelClosed(int, android.view.Menu)' on a null object reference
    at androidx.appcompat.app.AppCompatActivity.onPanelClosed(AppCompatActivity.java:505)
    at androidx.lifecycle.LiveData.observe(LiveData.java:172)
    at com.firebase.ui.auth.KickoffActivity.onCreate(KickoffActivity.java:35)
    at android.app.Activity.performCreate(Activity.java:6910)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:156) 
    at android.app.ActivityThread.main(ActivityThread.java:6524) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 

The weird thing is that following the stack the exception is onPanelClosed, which means super is null. How is this possible?

@Override
public void onPanelClosed(int featureId, Menu menu) {
    super.onPanelClosed(featureId, menu);
}

UPDATE: here is the activity code

package com.gabrielcalero.familytree.ui
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.firebase.ui.auth.AuthUI
import com.gabrielcalero.familytree.R
import com.gabrielcalero.familytree.ServiceLocator
import com.gabrielcalero.familytree.entity.Constants
import com.gabrielcalero.familytree.ui.picasso.CropCircleTransformation
import com.google.android.material.navigation.NavigationView
import com.google.firebase.auth.FirebaseAuth
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, FirebaseAuth.AuthStateListener {

private var mUserPicture: ImageView? = null
private var mUserNameTextView: TextView? = null
private var mAuth: FirebaseAuth? = null

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

    mUserPicture = navView.getHeaderView(0).findViewById(R.id.picture)
    mUserNameTextView = navView.getHeaderView(0).findViewById(R.id.display_name)

    mAuth = FirebaseAuth.getInstance().apply { addAuthStateListener { this } }

    launchSignInUI()
}

override fun onStart() {
    super.onStart()
    updateLoggedInUser()
}

private fun updateLoggedInUser() {
    val currentUser = mAuth!!.currentUser
    if (currentUser == null) {
        mUserPicture!!.setImageResource(R.drawable.anonymous)
        mUserNameTextView!!.text = getString(R.string.not_logged_in)
        navView.menu.findItem(R.id.signout).isVisible = false
        navView.menu.findItem(R.id.signin).isVisible = true
        navView.menu.findItem(R.id.personFragment).isEnabled = false
        navView.menu.findItem(R.id.peopleListFragment).isEnabled = false
    } else {
        // show signout
        mUserNameTextView!!.text = currentUser.displayName
        Picasso.with(this)
                .load(currentUser.photoUrl)
                .placeholder(R.drawable.anonymous)
                .error(R.drawable.anonymous)
                .transform(CropCircleTransformation())
                .resize(resources.getDimension(R.dimen.profile_picture_width).toInt(),
                        resources.getDimension(R.dimen.profile_picture_height).toInt())
                .into(mUserPicture)
        navView.menu.findItem(R.id.signout).isVisible = true
        navView.menu.findItem(R.id.signin).isVisible = false
        navView.menu.findItem(R.id.personFragment).isEnabled = true
        navView.menu.findItem(R.id.peopleListFragment).isEnabled = true
    }
}

private fun setupNavigation() {
    setSupportActionBar(toolbar)

    supportActionBar!!.setDisplayShowHomeEnabled(true)
    supportActionBar!!.setDisplayHomeAsUpEnabled(true)

    val navController : NavController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
    toolbar.setupWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
    navView.setNavigationItemSelectedListener(this)
}

private fun launchSignInUI() {
    // Choose authentication providers
    val providers = Arrays.asList(
            AuthUI.IdpConfig.EmailBuilder().build(),
            AuthUI.IdpConfig.GoogleBuilder().build())

    // Create and launch sign-in intent
    startActivityForResult(
            AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(providers)
                    .setLogo(R.drawable.logo)
                    .build(),
            Constants.REQUEST_CODE_SIGN_IN)
}


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

override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
    val navController = findNavController(R.id.nav_host_fragment)
    when (menuItem.itemId) {
        R.id.signout -> signout()
        R.id.signin -> launchSignInUI()
        R.id.personFragment -> {
            navController.popBackStack(R.id.peopleListFragment, false)
            navController.navigate(R.id.personFragment)
            drawerLayout?.closeDrawers()
            return true
        }
        else -> {
            navController.popBackStack(R.id.peopleListFragment, false)
            drawerLayout?.closeDrawers()
            return true
        }
    }
    drawerLayout?.closeDrawers()
    return true
}

private fun signout() {
    val navController = findNavController(R.id.nav_host_fragment)

    AuthUI.getInstance()
            .signOut(this)
            .addOnCompleteListener {
                navView.menu.findItem(R.id.signout).isVisible = false
                navController.popBackStack(R.id.peopleListFragment, false)
            }

}

override fun onAuthStateChanged(firebaseAuth: FirebaseAuth) {
    ServiceLocator.getRepository(application).setCurrentUserId(firebaseAuth.uid)
    updateLoggedInUser()
}

}
gabocalero
  • 473
  • 6
  • 15
  • UPDATE: The issue was similar to this one https://stackoverflow.com/questions/53899751/google-auth-migrating-to-androidx-signinhubactivity-does-not-implement-lifecycl I've fixed it by removing this dependency implementation "androidx.core:core-ktx:+" – gabocalero Jul 10 '19 at 19:20

1 Answers1

0

The issue was in Java to Kotlin conversion. In this case I've fixed it by removing the unused dependency

implementation "androidx.core:core-ktx:+"

I guess when I finish converting all java classes I will have to replace all androidx dependencies by their -ktx counterpart

gabocalero
  • 473
  • 6
  • 15