registerReceiver() shows error for overloaded method

602 views Asked by At

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

enter image description here

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?

1

There are 1 answers

0
Stephan On BEST ANSWER

First the parameter is named filter and not intentFilter 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 method public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) is from Android and written in java. Removing the names should work fine:

registerReceiver(
            networkChangeReceiver,
            IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
    )