繁体   English   中英

我如何避免Kotlin中的null属性

[英]How can i avoid null properties in Kotlin

我从现在开始已经使用Kotlin,但是我无法为Kotlin中的所有属性实现非空类型。

请看下面的代码,在某些情况下我必须使用null类型。 我知道我可以使用lateinit但在某些情况下不适合。 如何避免代码中出现null?

如果任何人都可以使用没有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
}

暂无
暂无

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

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