Android - declare and use views in Kotlin

I am trying to use kotlin for android and am trying to declare Linringayout 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 the log I get 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.

+3


source to share


6 answers


Your problem is nullability and it would be nice to use the keyword lateinit

(documentation) :

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

      

This way you are defining a variable with a non-null value, but delaying initialization, which you can do in onCreate ().
You need to initialize it before you can access it, or you will PropertyNotInitialisedException

.



The second option is lazy initialization using property delegation :

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

      

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

+5


source


Using the operator !!

, checks if the value it applies null

to and if it does, it throws away KotlinNullPointerException

; otherwise, it returns a non-nullable type. The notation null!!

is mainly abbreviated for throw KotlinNullPointerException(...)

.

For Android views (and in other cases where an object is initialized in a special init method instead of a constructor), you must use the lateinit

keyword:



internal lateinit var linlay_SuccessfulPayment: LinearLayout
internal lateinit var linlay_failPayment: LinearLayout

      

This allows you to have nullable properties in Activity

that you don't initialize when you call the constructor, but only later in the method onCreate

. However, in this case, you take responsibility for initializing the variables before using them the first time, otherwise you will get a runtime exception.

+3


source


I'm just starting out with Kotlin and trying to learn it. I am linking views like this:

var myBtn: Button = findViewById<Button>(R.id.btnMyButton) as Button

      

or

var btnLogin: Button = findViewById<Button>(R.id.btnLogin)

      

+1


source


Initialize as shown below

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

      

and use it like below

linlay_SuccessfulPayment = findViewById<LinearLayout>(R.id.linlay_SuccessfulPayment)
linlay_failPayment = findViewById<LinearLayout>(R.id.linlay_failPayment)

      

0


source


You can use as below: -

 var linlay_SuccessfulPayment : LinearLayout? = null
 var linlay_failPayment : LinearLayout? = null


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

      

0


source


You can use apply plugin: 'kotlin-android-extensions'

build.gradle at the application level to link the view without using boilerplate code findViewById()

.
e.g. linlay_failPayment.setParams()//any thing you want with only view_id


For more information check this kotlin-view-binding

0


source







All Articles