Martin Erlic
Martin Erlic

Reputation: 5667

Operator == cannot be applied to 'Long' and 'Int' in Kotlin

I'm attempting to implement parts of Mike Penz' NavigationDrawer (https://github.com/mikepenz/MaterialDrawer) in Kotlin. Since then I've run into only a few issues, primarily with operators. Here is part of the code to instantiate the drawer itself. Android Studio doesn't throw any errors except where I'm using the == operator on int and Long variables:

        // Create the Drawer
        result = DrawerBuilder()
                .withSliderBackgroundColor(ContextCompat.getColor(applicationContext, R.color.top_header))
                .withActivity(this)
                .withToolbar(toolbar)
                .withHasStableIds(true)
                .withItemAnimator(AlphaCrossFadeAnimator())
                .withAccountHeader(headerResult!!)
                .addDrawerItems(
                        PrimaryDrawerItem().withName(R.string.drawer_item_profile).withIcon(FontAwesome.Icon.faw_user).withIdentifier(1).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_create).withIcon(FontAwesome.Icon.faw_paint_brush).withIdentifier(2).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_yaanich_news).withIcon(FontAwesome.Icon.faw_newspaper_o).withIdentifier(3).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_my_groups).withIcon(FontAwesome.Icon.faw_users).withIdentifier(4).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog).withIdentifier(5).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke))
                )
                .withOnDrawerItemClickListener { view, position, drawerItem ->

                    if (drawerItem != null) {
                        var intent: Intent? = null
                        if (drawerItem.identifier == (1) {
                            intent = Intent(this, UserProfileActivity::class.java)
                        } else if (drawerItem.identifier == 2) {
                            intent = Intent(this, YeetActivity::class.java)
                        } else if (drawerItem.identifier == 3) {
                            intent = Intent(this, RssActivity::class.java)
                        } else if (drawerItem.identifier == 4) {
                            intent = Intent(this, GroupsActivity::class.java)
                        } else if (drawerItem.identifier == 5) {
                            intent = Intent(this, UserSettingsActivity::class.java)
                        }
                        if (intent != null) {
                            this.startActivity(intent)
                        }
                    }
                    false
                }
                .withSavedInstance(savedInstanceState)
                .withShowDrawerOnFirstLaunch(true)
                .build()

        RecyclerViewCacheUtil<IDrawerItem<*, *>>().withCacheSize(2).apply(result!!.recyclerView, result!!.drawerItems)

        if (savedInstanceState == null) {
            result!!.setSelection(21, false)
            headerResult!!.activeProfile = profile
        }
    }

Errors:

if (drawerItem.identifier == (1)

if (drawerItem.identifier == 2)

Operator == cannot be applied to 'Long and' 'Int'

Upvotes: 63

Views: 54269

Answers (5)

Jalees Mukarram
Jalees Mukarram

Reputation: 116

Make sure when you are comparing Double with any number like myDouble == 5 it will not work until you add .0 at the end. It is because Kotlin guesses the type from the entered number and it will consider 5 as int and myDouble as double and will product same error

So, always explicitly make the comparing variable same of other variable

drawerItem.identifier == (1.0)

Upvotes: 0

Sarang Srivastava
Sarang Srivastava

Reputation: 21

If the above solution didn't work for you, try explicitly converting the value on right to the particular wider type on left side

num = 5.7
if (num == 4.toDouble()) {
    //some code
}

Every number type supports the following conversions:

  • toByte(): Byte
  • toShort(): Short
  • toInt(): Int
  • toLong(): Long
  • toFloat(): Float
  • toDouble(): Double
  • toChar(): Char

Upvotes: 2

Francesc
Francesc

Reputation: 29360

Simply use long on your right side

if (drawerItem.identifier == 1L)

Edit: the reason this is required is that Kotlin does not promote Ints to Longs (or, more generally, does not widen types); on the left side we had a Long and on the right side we had an Int, which lead to the error. Explicitly indicating that the right side is a Long fixes the error.

Upvotes: 173

cfl
cfl

Reputation: 1059

For interest, another solution would be to use compareTo(). compareTo returns zero if the values are equal, negative if its less than the other, or positive if its greater than the other:

 if(drawerItem.identifier.compareTo(1) == 0)   "Equals"

Upvotes: 2

Ranajit Sawant
Ranajit Sawant

Reputation: 201

if you facing problem of < operator != not applied to long and int > solve it Simply using long on your right side ie value!= 1L thats it..

Upvotes: 0

Related Questions