'How to define a variable which implement the interface in Kotlin?
I hope to define a variable mAction
which implement the interface MediaRecorder.OnInfoListener
.
But Code A isn't correct, how can I fix it?
Code A
val mAction: MediaRecorder.OnInfoListener{
mr, what, extra ->
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
//ToDo
}
}
Added Content:
Code B
val mAction = object: MediaRecorder.OnInfoListener{
override fun onInfo(mr: MediaRecorder?, what: Int, extra: Int) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
}
}
}
Solution 1:[1]
You are simply missing an =
instead of :
.
val mAction = MediaRecorder.OnInfoListener{
mr, what, extra ->
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
//ToDo
}
}
You can’t define a property without =
unless you are defining a custom getter or using a property delegate.
Solution 2:[2]
val mAction = MediaRecorder.OnInfoListener{
mr, what, extra ->
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
//ToDo
}
}
you can achieve this by doing something like this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |