onCreate从Java转换为Kotlin后崩溃

时间:2017-08-08 07:53:38

标签: java android exception kotlin oncreate

我将我的Launcher Activity从Java转换为Kotlin,我在onCreate()中遇到了崩溃,我不明白它崩溃的原因。

爪哇:

@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) 

1 个答案:

答案 0 :(得分:3)

onActivityCreated中的AppLifecycleTracker方法看起来有一个参数savedInstanceState: Bundle,该参数应为savedInstanceState: Bundle?,因为在创建活动时不一定有可用的已保存状态,这意味着参数值可以是null。 Kotlin检查这一点以确保null safety

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