我现在使用Kotlin很长一段时间了,但我无法为Kotlin中的所有属性实现非null类型。
请看下面的代码,在某些情况下我必须使用null类型。我知道我可以使用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
}
}
答案 0 :(得分:1)
我想你可能需要一些
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
}