Android-在Kotlin中声明和使用视图 [英] Android - Declare and use views in Kotlin

查看:85
本文介绍了Android-在Kotlin中声明和使用视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将kotlin用于android,并尝试这样声明Linearlayout:

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) 

请帮帮我.

推荐答案

您的问题是具有可为空性,这将是使用lateinit关键字

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

这样,您可以定义一个不可为空的var,但是会延迟初始化,您可以在onCreate()中进行此操作.
您必须先对其进行初始化,然后才能访问它.否则将得到一个PropertyNotInitialisedException.

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.

第二种选择是使用财产委派:

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.

这篇关于Android-在Kotlin中声明和使用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆