从 Java 转换为 Kotlin 后 onCreate 崩溃

onCreate crashing after converting from Java to Kotlin

我将 Launcher Activity 从 Java 转换为 Kotlin,但我在 onCreate() 中发生崩溃,我不明白为什么会崩溃。

Java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

科特林:

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

调用堆栈:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a.b.c/com.a.b.c.Authentication.AuthenticationActivity}: 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:2416)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                   Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
                                                                      at com.a.b.c.Activity.AppLifecycleTracker.onActivityCreated(AppLifecycleTracker.kt)
                                                                      at android.app.Application.dispatchActivityCreated(Application.java:195)
                                                                      at android.app.Activity.onCreate(Activity.java:922)
                                                                      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:277)
                                                                      at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
                                                                      at com.a.b.c.Authentication.AuthenticationActivity.onCreate(AuthenticationActivity.kt:147)
                                                                      at android.app.Activity.performCreate(Activity.java:6251)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

看起来 AppLifecycleTracker 中的方法 onActivityCreated 有一个参数 savedInstanceState: Bundle,它应该是 savedInstanceState: Bundle?,因为在 activity创建,也就是说参数值可以是null。 Kotlin 对此进行检查以确保 null safety.

崩溃似乎与 Activity 本身的转换无关。