Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Declare and use views in Kotlin

Tags:

android

kotlin

I am trying to use kotlin for android and trying to declare Linearlayout like this:

internal var linlay_SuccessfulPayment : LinearLayout = null!!
internal var linlay_failPayment : LinearLayout

linlay_SuccessfulPayment = findViewById(R.id.linlay_SuccessfulPayment) as LinearLayout
linlay_failPayment = findViewById(R.id.linlay_failPayment) as LinearLayout

But in log I am getting this :

Caused by: kotlin.KotlinNullPointerException
                                                                         at com.example.activities.PaymentResult.<init>(Result.kt:14)
                                                                         at java.lang.Class.newInstance(Native Method)
                                                                         at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7329) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Please help me out.

like image 356
Apurva Kolapkar Avatar asked May 30 '17 11:05

Apurva Kolapkar


1 Answers

Your problem is with nullability and this would be a good place to use the lateinit keyword (documentation):

private lateinit var linlay_SuccessfulPayment: LinearLayout
private lateinit var linlay_failPayment: LinearLayout

This way you define a non-nullable var but delay the initialisation, which you can do in onCreate().
You do have to initialise it before accessing it or you will get a PropertyNotInitialisedException.

A second option is lazy initialisation using property delegation:

private var linlay_SuccessfulPayment: LinearLayout by Delegates.lazy { findViewById(R.id.linlay_SuccessfulPayment) as LinearLayout }

This way the view is initialised only the first time it is used and you have everything in one place.

like image 124
RobCo Avatar answered Sep 28 '22 06:09

RobCo