我怎样才能避免在Kotlin空属性

我从一段时间以来一直在使用Kotlin,但是我无法实现Kotlin中所有属性的非空types。

请看下面的代码,有一些场景我必须使用空types。 我知道我可以使用lateinit但在某些情况下,它不适合。 我怎样才能在我的代码中避免null?

如果任何人都可以重新编写没有空types的代码或纠正我的错误,对我来说,了解所有的东西已经足够了。

 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 }