Android 如何避免Kotlin中的null属性
我使用Kotlin已有一段时间了,但我无法为Kotlin中的所有属性实现非空类型 请看下面的代码,有一些场景我必须使用空类型。我知道我可以使用Android 如何避免Kotlin中的null属性,android,kotlin,Android,Kotlin,我使用Kotlin已有一段时间了,但我无法为Kotlin中的所有属性实现非空类型 请看下面的代码,有一些场景我必须使用空类型。我知道我可以使用lateinit,但在某些情况下,它并不适合。如何在代码中避免null 如果有人可以用空类型重新编写代码,或者纠正我的错误,这就足以让我理解一切 class MusicService : Service(), PlaybackManager.PlaybackServiceCallback { private val mDelayedStopHan
lateinit
,但在某些情况下,它并不适合。如何在代码中避免null
如果有人可以用空类型重新编写代码,或者纠正我的错误,这就足以让我理解一切
class MusicService : Service(), PlaybackManager.PlaybackServiceCallback {
private val mDelayedStopHandler = DelayedStopHandler(this)
private val eventBus = EventBus.getDefault()
//How to avoid nullable types
private var mMediaNotificationManager: MediaNotificationManager? = null
private var mSession: MediaSessionCompat? = null
var mSessionToken: MediaSessionCompat.Token? = null
var mPlaybackManager: PlaybackManager? = null
var mTransportControls: MediaControllerCompat.TransportControls? = null
override fun onCreate() {
Timber.d("onCreate")
super.onCreate()
//Init MediaSessionCompat and TransportControls
mSession = MediaSessionCompat(this, "MusicService")
mSessionToken = mSession?.sessionToken
mSession?.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
mTransportControls = MediaControllerCompat(this, mSession).transportControls
//EventBus Reg
eventBus.reg(this)
eventBus.post(GetAllMediaEventRequest())
}
@Subscribe
fun onGetAllMediaEventResponse(event: GetAllMediaEventResponse) {
Timber.d("GetAllMediaEventResponse event.status = ", event.status)
//init PlaybackManager
mPlaybackManager = PlaybackManager(mPlayback = LocalPlayer(this),
mMediaData = event.mediaItems,
mServiceCallback = this)
mSession?.setCallback(mPlaybackManager!!.mMediaSessionCallback)
//Init Notification
try {
mMediaNotificationManager = MediaNotificationManager(this)
} catch (e: RemoteException) {
throw IllegalStateException("Could not create a MediaNotificationManager", e)
}
}
}
更新:
谢谢你给我的回复。经过一点研究,我使所有属性都不可为空。请检查我的代码,如果有错误请更正
class MusicService : Service(), PlaybackManager.PlaybackServiceCallback {
//NotNull
private val mDelayedStopHandler = DelayedStopHandler(this)
private val eventBus = EventBus.getDefault()
//Lateinit
lateinit var mSessionToken: MediaSessionCompat.Token
lateinit var mTransportControls: MediaControllerCompat.TransportControls
//Lazy
private val mSession: MediaSessionCompat by lazy { MediaSessionCompat(this, "MusicService") }
private val mMediaNotificationManager: MediaNotificationManager by lazy {
try {
MediaNotificationManager(this)
} catch (e: RemoteException) {
throw IllegalStateException("Could not create a MediaNotificationManager", e)
}
}
val mPlaybackManager: PlaybackManager by lazy {
PlaybackManager(mPlayback = LocalPlayer(this), mServiceCallback = this)
}
override fun onCreate() {
LogHelper.d(TAG, "onCreate")
super.onCreate()
//Init MediaSessionCompat and TransportControls
mSessionToken = mSession.sessionToken
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
mTransportControls = MediaControllerCompat(this, mSession).transportControls
mSession.setCallback(mPlaybackManager.mMediaSessionCallback)
//EventBus Reg
eventBus.reg(this)
eventBus.post(GetAllMediaEventRequest())
}
@Subscribe
fun onGetAllMediaEventResponse(event: GetAllMediaEventResponse) {
Timber.d("GetAllMediaEventResponse event.status = ", event.status)
mPlaybackManager.mMediaData = event.mediaItems
}
}
我想你可能需要一些
a?.let {
println(it)
// if `a` isn't null, the code will reach here
// and `it` will hold the value of `a`
// you can do a lot of things here without checking if it is null
}
lateinit
实际上非常适合在onCreate
中初始化的所有属性。其余的似乎应该是可空的,因为它们在调用onGetAllMediaEventResponse
之前不会初始化。@Naetmul这不是真的,lateinit并不意味着不能多次赋值。这就是val
的作用。