简体   繁体   中英

Android kotlin onCreate(savedInstanceState: Bundle?) cause IllegalArgumentException

The first Activity doesn't launch nevertheless Bundle? .

class FirstActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

}

Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter bundle

This error should occur when override fun onCreate(savedInstanceState: Bundle)

However, I fixed override fun onCreate(savedInstanceState: Bundle?)

That's error occurred at Application class, not Activity.

Need to change Bundle into "Bundle?" both Activity and Application class.

  override fun onActivityCreated(activity: Activity, bundle: Bundle?) {

    }

  override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle?) {}

I had same error when i exchange .java to .kt automatically. But the real wrong point wasn't in Activity. In my case, the wrong place was ActivityLifecycleCallbacks where some parameters missing '?' in exchanging.

Wrong:

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) 
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle)

Correct:

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) 
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM