[英]How can i refrence variable inside outer class (kotlin)
How can i reference the SongName variable in my other object? 如何在我的其他对象中引用SongName变量? I am building my first app with Kotlin so I am really beginner. 我正在用Kotlin构建我的第一个应用程序,所以我真的很初学。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
SpotifyService.connect(this) {
spotifyAppRemote?.let {
it.playerApi.subscribeToPlayerState().setEventCallback {
val track: Track = it.track
Log.d("MainActivity", track.name + " by " + track.artist.name)
ALabel.text = track.name
Blabel.text = track.artist.name
var SongName = track.name
var SongArtist = track.artist.name
}
}
}
GeniusApi.PrintSomething()
}
}``` ```object GeniusApi {
val BASE_URL = "https://api.genius.com"
val SEARCH_URL = BASE_URL + "/search"
var DATA = SongName@MainActivity
fun PrintSomething() {
Log.e("GeniusApi", DATA)
}
}```
On your Kotlin file you will need to place any functions or variables inside a 在您的Kotlin文件中,您需要将任何函数或变量放在一个
Companion Object {
lateinit var myobject
}
And then reference it like 然后像它一样引用它
TheObject.Companion.myobject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.