简体   繁体   English

Android - 在 Kotlin 中声明和使用视图

[英]Android - Declare and use views in Kotlin

I am trying to use kotlin for android and trying to declare Linearlayout like this:我正在尝试将 kotlin 用于 android 并尝试像这样声明 Linearlayout:

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.请帮帮我。

Your problem is with nullability and this would be a good place to use the lateinit keyword (documentation) :您的问题是可空性,这将是使用lateinit关键字(文档)的好地方:

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().通过这种方式,您可以定义一个不可为空的 var 但延迟初始化,您可以在 onCreate() 中执行此操作。
You do have to initialise it before accessing it or you will get a PropertyNotInitialisedException .你必须在访问它之前初始化它,否则你会得到一个PropertyNotInitialisedException

A second option is lazy initialisation usingproperty 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.这样,视图仅在第一次使用时初始化,并且您将所有内容都放在一个地方。

Using the !!使用!! operator checks if the value it's applied to is null , and if it is, it throws a KotlinNullPointerException ;运算符检查它应用的值是否为null ,如果是,则抛出KotlinNullPointerException otherwise it returns the value with a non-nullable type.否则它返回一个不可为空类型的值。 Writing down null!!写下来为null!! is basically shorthand for throw KotlinNullPointerException(...) .基本上是throw KotlinNullPointerException(...)简写。

For Android Views (and other cases where an object is initialized in a special init method instead of the constructor), you should use the lateinit keyword:对于 Android 视图(以及其他在特殊 init 方法而不是构造函数中初始化对象的情况),您应该使用lateinit关键字:

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

This lets you have non-nullable properties in your Activity that you don't initialize when the constructor is called, but only later, in the onCreate method.这使您可以在Activity中拥有不可为 null 的属性,这些属性在调用构造函数时不会初始化,但只能稍后在onCreate方法中进行初始化。 You do however, in this case, take responsibility for initializing the variables before using them the first time, otherwise you'll get an exception at runtime.但是,在这种情况下,您需要在第一次使用变量之前对其进行初始化,否则您将在运行时遇到异常。

只需在 gradle.build 中添加 kotlin 扩展,您就不需要初始化参数:

apply plugin: 'kotlin-android-extensions'

Initialize like 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)

I'm just starting with Kotlin and trying to learn it.我刚刚开始使用 Kotlin 并尝试学习它。 I bind views like this:我绑定这样的视图:

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

or

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

You can use like 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

You can use apply plugin: 'kotlin-android-extensions' in your app level build.gradle for binding view without using the findViewById() boilerplate code.您可以在应用级 build.gradle 中使用apply plugin: 'kotlin-android-extensions'来绑定视图,而无需使用findViewById()样板代码。
eg linlay_failPayment.setParams() // any thing you want with only view_id例如linlay_failPayment.setParams() // any thing you want with only view_id
For more information check this kotlin-view-binding有关更多信息,请查看此kotlin-view-binding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM