I was trying Kotlin in my Android project. There is a broadcast receiver for network change events in my activity. Code is as below:
BaseActivity.kt
abstract class BaseActivity : AppCompatActivity() {
private val networkChangeReceiver = NetworkChangeReceiver()
override fun onStart() {
super.onStart()
registerReceiver(
receiver = networkChangeReceiver,
intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
override fun onStop() {
super.onStop()
unregisterReceiver(receiver = networkChangeReceiver)
}
}
NetworkChangeReceiver.kt
class NetworkChangeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.e("network changed")
}
}
I don't see any problem in it. But my kotlin plugin shows me following error:
None of following functions can be called with arguments supplied
The arguments supplied for first one is correct ASAFIK. I am not an expert in Kotlin, just learning it for fun. Is this intended behaviour of Kotlin, error in plugin or am I missing something? Can anyone explain?
First the parameter is named
filter
and notintentFilter
and if you correct this, you get another error. Which says: "Named arguments are not allowed for non-Kotlin functions" Reason for this is that the methodpublic Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
is from Android and written in java. Removing the names should work fine: