运算符==不能应用于Kotlin中的'Long'和'Int'

Mar*_*lic 28 android operator-keyword kotlin

我正在尝试在Kotlin中实现Mike Penz的NavigationDrawer(https://github.com/mikepenz/MaterialDrawer)的部分内容.从那以后,我遇到了一些问题,主要是运营商.以下是实例化抽屉本身的代码的一部分.Android Studio不会抛出任何错误,除非我在int和Long变量上使用==运算符:

        // 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
        }
    }
Run Code Online (Sandbox Code Playgroud)

错误:

if (drawerItem.identifier == (1)

if (drawerItem.identifier == 2)

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

Fra*_*esc 82

只需在右侧使用很长时间即可

if (drawerItem.identifier == 1L)
Run Code Online (Sandbox Code Playgroud)

编辑:这是必要的原因是Kotlin不会将Ints推广到Longs(或者更常见的是,不会扩展类型); 在左侧我们有一个Long,在右侧我们有一个Int,导致错误.明确指示右侧是Long修复错误.

  • “Kotlin 不会提升/拓宽类型”这一说法在多大程度上是正确的?其他东西确实有效,比如有序比较和 elvis 运算符。例如,“if (someLongValue &gt; 1)”有效(同上“&gt;=”、“&lt;”和“&lt;=”),而“someNullableLongValue ?: 0”有效且类型为“Long”。 (2认同)
  • @kbolino 没有类型提升,但大多数操作(例如 &gt;=、&lt; 等)实际上是函数,它们对所有类型组合进行重载以执行正确的操作 (2认同)