Reputation: 395
I am trying to convert a java function to its kotlin counterpart but I am getting Modifier 'override' is not applicable to 'local function'
at override fun binderDied()
.
This is the java code:
private void linkToDeath(IBinder service) {
try {
service.linkToDeath(new IBinder.DeathRecipient() {
@Override
public void binderDied() {
Log.d(TAG, "======binderDied======");
deviceServiceEngine = null;
bindDeviceService();
}
}, 0);
} catch (RemoteException e) {
e.printStackTrace();
}
}
And the kotlin conversion that is complaining at override fun binderDied()
private fun linkToDeath(service: IBinder) {
try {
service.linkToDeath(IBinder.DeathRecipient {
override fun binderDied() {
Log.d(TAG, "======binderDied======")
deviceServiceEngine = null
bindDeviceService()
}
}, 0)
} catch (e: RemoteException) {
e.printStackTrace();
}
}
What am I missing here? Thanks in advance.
Upvotes: 1
Views: 1028
Reputation: 93902
The Java version has an anonymous class implementation of the interface (I'm assuming it's an interface and not a class). To do this in Kotlin, you need to precede it with object:
like this:
private fun linkToDeath(service: IBinder) {
try {
service.linkToDeath(object: IBinder.DeathRecipient {
override fun binderDied() {
Log.d(TAG, "======binderDied======")
deviceServiceEngine = null
bindDeviceService()
}
}, 0)
} catch (e: RemoteException) {
e.printStackTrace();
}
}
If this interface is a functional interface (only one abstract method), then you can use lambda syntax instead of an anonymous class:
private fun linkToDeath(service: IBinder) {
try {
service.linkToDeath(IBinder.DeathRecipient {
Log.d(TAG, "======binderDied======")
deviceServiceEngine = null
bindDeviceService()
}, 0)
} catch (e: RemoteException) {
e.printStackTrace();
}
}
You got that error because without object:
you were using the lambda syntax, so your inner function was parsed as a locally defined function within the lambda function.
Upvotes: 1