Institutions in Kotlin

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

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

But I'm writing a project using Kotlin, so he likes Kotlin

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

But he falls with a 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) 
+7
android android-intent kotlin
source share
7 answers

Make sure you have ( question mark ):

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

Instead

 override fun onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) } 
+11
source share

The intentions using Kotlin for Android are almost the same that we just need to change the syntax a bit, as shown below.

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

The exception you get is the null pointer exception in your onCreate method for activity, please check the same.

+11
source share

Anko also provides some interesting functions for moving to another activity without the need to create an intention, add additional functions, or call a function.

Everything can be done in one line:

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

This will create a set of additional functions for the intent with the values ​​specified in the list of pairs that the function takes as a parameter.

+2
source share

Intentions in android kotlin can be used as follows.

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

Also, the exception you get exists in your onCreate method.

+2
source share

You have a null pointer exception in the onCreate method of the new action. Check onCreate method.

0
source share
  • Create a Singleton class for the 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 calling it where you need an instance of the application object. eg:

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

The shortest and easiest way I use to open a new action

 startActivity(Intent( this@MainActivity , NextActivity::class.java)) 

PS: One of the highlights that should be noted here is that Intent () expects packageContext to be the first parameter, so just this one throws an error anyway.

Therefore, you need to explicitly define the class name. As shown in the line above (this is @MainActivity)

0
source share

All Articles