2017-05-24 52 views
-2

我從一段時間以來一直在使用Kotlin,但是我無法實現Kotlin中所有屬性的非空類型。我該如何避免Kotlin中的空屬性

請看下面的代碼,有一些場景我必須使用空類型。我知道我可以使用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 
    } 
} 
+2

'lateinit'將適合所有你在'onCreate'初始化很好的性能,其實。剩下的看起來應該是可以爲空的,因爲它們在調用onGetAllMediaEventResponse之前不會被初始化。 – zsmb13

+1

@Naetmul這不是事實,lateinit並不意味着你不能分配多次。這就是'val'的意思。 – Robin

回答

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 
} 
相關問題