Hi I have a Java code (I have zero experience in Java) which I want to rewrite in Kotlin (under a month of experience). If I’m not mistaken this is a typical singleton implementation in Java:
public class LinphoneService extends Service {
private static LinphoneService sInstance;
private Core mCore;
public static LinphoneService getInstance() {
return sInstance;
}
public static Core getCore() {
return sInstance.mCore;
}
}
Code written by myself doesn’t work so I decided to use java to kotlin converter inside Android Studio:
open class LinphoneService : Service() {
var core: Core? = null
// ERROR: unresolved reference for instance field
get() = instance.field
private set
companion object {
fun getCore(): Core? = instance?.core
var instance: LinphoneService? = null
private set
}
init {
sInstance = this
}
mCoreListener = object : CoreListenerStub() {
override fun onCallStateChanged(
core: Core,
call: Call,
state: Call.State,
message: String
) {
if (state == Call.State.IncomingReceived) {
// ERROR: here I have unresolved reference core
val params: CallParams = Companion.core.createCallParams(call)
} else if (state == Call.State.Connected) {
//
}
}
}
}
Here I have two errors: unresolved reference for instance field and unresolved reference for Companion object. How can I solve this issues? Thanks.
Should I replace instance.field
by instance?.core
?
Should I replace Companion.core.createCallParams(call)
by instance?.core?.createCallParams()
?
Or something else, because this variants still don’t work for me.