Home » Android » android – IllegalArgumentException: savedInstanceState Specified as Non-Null is Null

android – IllegalArgumentException: savedInstanceState Specified as Non-Null is Null

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am getting a strange error when I start my MainActivity:

06-16 16:01:05.193 2083-2083/? E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.android.example.github, PID: 2083
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.example.github/com.android.example.github.ui.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
            at android.app.ActivityThread.-wrap12(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6121)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
          Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
            at com.android.example.github.injection.AppInjector$init$1.onActivityCreated(AppInjector.kt)
            at android.app.Application.dispatchActivityCreated(Application.java:197)
            at android.app.Activity.onCreate(Activity.java:961)
            at android.support.v4.app.BaseFragmentActivityGingerbread.onCreate(BaseFragmentActivityGingerbread.java:54)
            at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
            at com.android.example.github.ui.MainActivity.onCreate(MainActivity.kt:20)
            at android.app.Activity.performCreate(Activity.java:6682)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) 
            at android.app.ActivityThread.-wrap12(ActivityThread.java) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6121) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

This is my MainActivity class:

class MainActivity : LifecycleActivity(), HasSupportFragmentInjector {

    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>
        @Inject set

    lateinit var navigationController: NavigationController

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

        if (savedInstanceState == null) {
            navigationController.navigateToSearch()
        }
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment> {
        return dispatchingAndroidInjector
    }

}

The error states that the parameter savedInstanceState is null, when it is specified as non-null; but it is nullable (savedInstanceState: Bundle?) and the onCreate() method is marked as @Nullable in the source.

I have not come across this error in any of my other Kotlin projects. I am using Kotlin version 1.1.2-5; and had the same error with 1.1.2-3.

How to&Answers:

It seems like the issue isn’t in the onCreate method. Try looking at com.android.example.github.injection.AppInjector$init$1.onActivityCreated(AppInjector.kt). I can’t tell if it’s a generated class, but it should give you an idea of what to do next.

Answer:

I had the exact same problem, you have to make nullable types here:

override fun onCreatePreferences(bundle: Bundle?, s: String?) {}

Answer:

The issue is in the AppInjector. I used the google GitHubBrowser sample like you did and ran into this same error. I copied the code over to my project and let Android Studio convert it to Kotlin. When it did this it didn’t properly specify the nullability of the savedInstanceState parameter which is what is causing the crash. Just make it optional like in the below code and it will work.

object AppInjector {
    fun init(app: App) {
        DaggerAppComponent.builder().application(app)
                .build().inject(app)
        app
                .registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
                    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                        handleActivity(activity)
                    }

                    override fun onActivityStarted(activity: Activity) {

                    }

                    override fun onActivityResumed(activity: Activity) {

                    }

                    override fun onActivityPaused(activity: Activity) {

                    }

                    override fun onActivityStopped(activity: Activity) {

                    }

                    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

                    }

                    override fun onActivityDestroyed(activity: Activity) {

                    }
                })
    }

    private fun handleActivity(activity: Activity) {
        if (activity is HasSupportFragmentInjector) {
            AndroidInjection.inject(activity)
        }
        (activity as? FragmentActivity)?.supportFragmentManager?.registerFragmentLifecycleCallbacks(
                object : FragmentManager.FragmentLifecycleCallbacks() {
                    override fun onFragmentCreated(fm: FragmentManager?, f: Fragment?,
                                                   savedInstanceState: Bundle?) {
                        if (f is Injectable) {
                            AndroidSupportInjection.inject(f)
                        }
                    }
                }, true)
    }
}

Answer:

You may use null-coalescing operator ?:. The savedInstanceState will be passed to the super.onCreate method only if it is @NonNull, otherwise, if it is null, an empty Bundle() will be passed.

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

Answer:

As @mcassiano noted, it appears the issue isn’t with your onCreate method but actually onActivityCreated. Two thoughts:

  • Due to the fact that libraries and such are in transition, for now you may have to just manually override onActivityCreated in your fragment and explicitly mark the Bundle as nullable: (savedInstanceState: Bundle?). You shouldn’t have to, but manually setting up the nullability may just help.
  • I notice that the crash goes all the way down to BaseFragmentActivityGingerbread – is this a result of using the LifecycleActivity? Or do you have other, older, out-of-date dependencies in your project?

Answer:

You know that inside function super.onCreate looks like public void onCreate(Bundle savedInstanceState) this saveInstanceState cannot be Null and may be calling function can be connect to orther functions. In my case, super.onCreate connects to onCreatePreferencesthen i fixed this problem, this code following to:

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

    addPreferencesFromResource(R.xml.preferences_pomodoro)
}

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

    if (context != null) {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context!!)
    }
}