Home > Software engineering >  Android Kotlin - app crash RuntimeException NullPointerException in MainActivity.I
Android Kotlin - app crash RuntimeException NullPointerException in MainActivity.I

Time:02-13

In the gogole play console I see that some of the users with android 10, 11 and 12 are getting this error:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:4031)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4197)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:103)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2434)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8633)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1133)
Caused by: java.lang.NullPointerException: 
  at com.domain.bla.MainActivity.I (MainActivity.java:30)
  at com.domain.bla.MainActivity.setNavi (MainActivity.java)
  at com.domain.bla.MainActivity.onCreate (MainActivity.java)
  at android.app.Activity.performCreate (Activity.java:8282)
  at android.app.Activity.performCreate (Activity.java:8262)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1329)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:4005)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4197)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:103)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2434)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8633)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1133)

I don't have the function i() in MainActivity or anywhere in the app, maybe in one of the libraries but not from me.

I have testing devices with android 11 and 12 but can't reproduce this error, everything works as it should on those.

Posting the whole MainActivity would be way too much sicne it's over 1300 lines.

This is the setNavi function:

private fun setNavi(gal: String){

    val toggle = ActionBarDrawerToggle(
        this, binding.drawerLayout, binding.contentMain.toolbar, 0, 0
    )

    binding.drawerLayout.addDrawerListener(toggle)
    toggle.syncState()
    binding.navView.setNavigationItemSelectedListener(this)

    if(gal == "latest"){
        binding.navView.setCheckedItem(R.id.nav_latest)
    }else{
        binding.navView.setCheckedItem(R.id.nav_top)
    }

    binding.navView.itemIconTintList = null

    binding.contentMain.btnOpenSearch.setOnClickListener {
        binding.drawerLayout.closeDrawer(GravityCompat.START, true)
        val i = Intent(this@MainActivity, SearchInput::class.java)
        this.startActivity(i)
    }


    val headerLayout = binding.navView.getHeaderView(0) // to set stuff in menu
    //headerLayout.btnUpload.setOnClickListener {

    val btnUpload: ConstraintLayout = headerLayout.findViewById(R.id.btnUpload)
    btnUpload.setOnClickListener {
        if(prefs.getLong("userid", 0) == 0.toLong()){
            val i = Intent(this, Login::class.java)
            this.startActivity(i)

        }else{
            binding.drawerLayout.closeDrawer(GravityCompat.START, true)

            val i = Intent(this, Upload::class.java)
            startActivity(i)
        }
    }

    setHeader()
}

maybe the problem is in there?

CodePudding user response:

This happens because your app has minify enabled in your release build, it obfuscates and optimizes your code, therefore will show things on different lines. You can have a more assertive stacktrace if you provide google play the txt file provided by your minify method. Read: https://developer.android.com/studio/build/shrink-code#decode-stack-trace

  • Related