Intents in Kotlin

2016-09-13 android android-intent 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) 

Answers

You're having a null pointer exception on the onCreate method of the new activity. Please check the onCreate method.

Ensure you have (question mark):

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

Instead:

override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
}
  1. Create Singleton class for application instance for example :

class MyApp: Application() {

override fun onCreate() {
    super.onCreate()
    instance = this
}

companion object {
    lateinit var instance: MyApp
        private set
}

}

  1. then after call it where you required application object instance. for example :

    val intent: Intent = Intent(MyApp.instance, DestinationActivity::class.java);
    MyApp.instance.startActivity(intent)
    

As explained on this page, Anko also provides some nice functions to navigate to other activity without the need of creating an intent, adding the extras or calling the function.

Everything can be done in a single line:

startActivity<DetailActivity>("id" to 2, "name" to "Kotlin")

This will create a set of extras for the intent with the values specified by the list of pairs the function is receiving as a parameter.

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..

Intents in android kotlin can be used as given below.

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

Further, the exception that you are getting exists in your onCreate method.

The shortest and the simplest way I use to open a new activity

startActivity(Intent([email protected], NextActivity::class.java))

P.S: One of the main points to notice here is that the Intent() is expecting a packageContext as the first parameter hence just using this will still throw an error.

Hence, you need to explicitly define the class name. As shown in the above line ([email protected])

You can try "context?.applicationContext" instead of "this"

val intent = Intent(context?.applicationContext,TimerActivity::class.java)
startActivity(intent)

You can call like this

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

Related