gpt4 book ai didi

android - Kotlin 中的 Intent

转载 作者:IT老高 更新时间:2023-10-28 13:29:29 25 4
gpt4 key购买 nike

所以,我知道在 Java 中我们使用它来启动新的 Activity

Intent intent = new Intent(this, SomeActivity.class);
startActivity(intent);

但我正在使用 Kotlin 编写一个项目,所以在 Kotlin 中就像

val intent = Intent(this,SomeActivity::class.java)
startActivity(intent)

但它会因问题而崩溃

FATAL EXCEPTION: main
Process: com.pashabred.passlin, PID: 15243
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pashabred.passlin/com.pashabred.passlin.Enterring}: kotlin.KotlinNullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: kotlin.KotlinNullPointerException
at com.pashabred.passlin.Enterring.onCreate(Enterring.kt:17)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

最佳答案

在 Android 上使用 Kotlin 的 Intent 几乎相同,我们只需要在语法上稍作更改,如下所示。

val intent = Intent(this,HelloActivity::class.java) startActivity(intent)

您得到的异常是 Activity 的 onCreate 方法中的空指针异常,请检查相同..

关于android - Kotlin 中的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com