Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intents in Kotlin

So, I know that in Java we use this to launch new Activity

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

But I'm writting a project using Kotlin, so in Kotlin it's like

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

But it crashes with problem

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)  
like image 888
Pavel Bredelev Avatar asked Sep 13 '16 04:09

Pavel Bredelev


People also ask

What are intents in Kotlin?

Android Intent is a messaging object used to request another app component to perform an action. Intent facilitates users to communicate with app component through several ways such as starting an activity, starting a service, delivering a broadcast receiver, etc.

What are the different types of intents?

There are two types of intents in android: implicit and explicit.

What are intents used for?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.

What are intents in Android system?

An Intent in the Android operating system is a software mechanism that allows users to coordinate the functions of different activities to achieve a task.


2 Answers

Intents using Kotlin for Android are almost same we just need to change slight in the syntax as below.

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

Exception that you are getting is null pointer exception in your onCreate method on the activity, please check the same..

like image 200
AaRiF Avatar answered Sep 28 '22 22:09

AaRiF


Ensure you have (question mark):

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

Instead:

override fun onCreate(savedInstanceState: Bundle) {     super.onCreate(savedInstanceState) } 
like image 37
klimat Avatar answered Sep 28 '22 21:09

klimat