我正在使用lateinit属性,以避免使用?进行连续的空值检查。运算符(operator)。我有很多在getViews()函数中首次分配的View属性。如果没有该功能,我的应用程序将因Kotlin代码中的NPE而崩溃。
在我看来,Lateinit属性基本上破坏了该语言的不错的null安全功能。我知道它们是在M13中引入的,因为它们提供了更好的框架支持,但这值得吗?
还是我在这里想念什么?
这是代码:
package com.attilapalfi.exceptional.ui
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.attilapalfi.exceptional.R
import com.attilapalfi.exceptional.dependency_injection.Injector
import com.attilapalfi.exceptional.model.Exception
import com.attilapalfi.exceptional.model.ExceptionType
import com.attilapalfi.exceptional.model.Friend
import com.attilapalfi.exceptional.persistence.*
import com.attilapalfi.exceptional.rest.ExceptionRestConnector
import com.attilapalfi.exceptional.ui.helpers.ViewHelper
import com.attilapalfi.exceptional.ui.question_views.QuestionYesNoClickListener
import com.google.android.gms.maps.MapView
import java.math.BigInteger
import javax.inject.Inject
public class ShowNotificationActivity : AppCompatActivity(), QuestionChangeListener {
@Inject
lateinit val exceptionTypeStore: ExceptionTypeStore
@Inject
lateinit val friendStore: FriendStore
@Inject
lateinit val imageCache: ImageCache
@Inject
lateinit val metadataStore: MetadataStore
@Inject
lateinit val viewHelper: ViewHelper
@Inject
lateinit val exceptionInstanceStore: ExceptionInstanceStore
@Inject
lateinit val exceptionRestConnector: ExceptionRestConnector
@Inject
lateinit val questionStore: QuestionStore
private lateinit var sender: Friend
private lateinit var exception: Exception
private lateinit var exceptionType: ExceptionType
private lateinit var exceptionNameView: TextView
private lateinit var exceptionDescView: TextView
private lateinit var senderImageView: ImageView
private lateinit var senderNameView: TextView
private lateinit var sendDateView: TextView
private lateinit var mapView: MapView
private lateinit var questionText: TextView
private lateinit var noButton: Button
private lateinit var yesButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_show_notification)
Injector.INSTANCE.applicationComponent.inject(this)
questionStore.addChangeListener(this)
getModelFromBundle()
getViews()
loadViewsWithData()
}
override fun onDestroy() {
super.onDestroy()
questionStore.removeChangeListener(this)
}
private fun getModelFromBundle() {
val bundle = intent.extras
val instanceId = BigInteger(bundle.getString("instanceId"))
exception = exceptionInstanceStore.findById(instanceId)
sender = friendStore.findById(exception.fromWho)
exceptionType = exceptionTypeStore.findById(exception.exceptionTypeId)
}
private fun getViews() {
exceptionNameView = findViewById(R.id.notif_full_exc_name) as TextView
exceptionDescView = findViewById(R.id.notif_exc_desc) as TextView
senderImageView = findViewById(R.id.notif_sender_image) as ImageView
senderNameView = findViewById(R.id.notif_sender_name) as TextView
sendDateView = findViewById(R.id.notif_sent_date) as TextView
mapView = findViewById(R.id.notif_map) as MapView
questionText = findViewById(R.id.notif_question_text) as TextView
noButton = findViewById(R.id.notif_question_no) as Button
yesButton = findViewById(R.id.notif_question_yes) as Button
}
private fun loadViewsWithData() {
exceptionNameView.text = exceptionType.prefix + "\n" + exceptionType.shortName
exceptionDescView.text = exceptionType.description
imageCache.setImageToView(sender, senderImageView)
senderNameView.text = viewHelper.getNameAndCity(exception, sender)
sendDateView.text = exception.date.toString()
loadQuestionToViews()
}
private fun loadQuestionToViews() {
if (exception.question.hasQuestion) {
showQuestionViews()
} else {
hideQuestionViews()
}
}
private fun showQuestionViews() {
questionText.text = exception.question.text
val listener = QuestionYesNoClickListener(exception, exceptionRestConnector, noButton, yesButton)
noButton.setOnClickListener(listener)
yesButton.setOnClickListener(listener)
}
private fun hideQuestionViews() {
questionText.visibility = View.INVISIBLE
noButton.visibility = View.INVISIBLE
yesButton.visibility = View.INVISIBLE
}
override fun onQuestionsChanged() {
onBackPressed()
}
}
最佳答案
实际上,使用M13之前的Delegates.notNull可以实现lateinit的相同基本功能。
还有其他功能也使您可以绕过可空性实现。 !!运算符会将可为空的值转换为非空值。
关键不是严格要求可空性约束,而是使可空性成为语言中非常明确的一部分。每次您使用lateinit或!!您正在做出有意识的决定,离开安全性约束的安全性,希望有充分的理由。
根据经验,最好避免使用Lateinit,!!,甚至? (可为空)。
很多时候,您可以使用lazy委托(delegate)来避免lateinit,后者可能使您陷入非null val的境界(M14现在禁止将vals与lateinit一起使用)。
您链接到的代码包含许多Lateinit View 。您可以通过执行以下操作将它们保留为非空值:
private val mapView: MapView by lazy { findViewById(R.id.notif_map) as MapView }
这将在第一次使用mapView时初始化该值,此后使用先前初始化的值。需要注意的是,如果您在调用setContentView之前尝试使用mapView,则可能会中断。但是,这似乎没什么大不了的,并且您已经获得了初始化就在声明旁边的好处。
这就是kotterknife library用于实现 View 注入(inject)的功能。