【问题标题】:Operator == cannot be applied to 'Long' and 'Int' in Kotlin运算符 == 不能应用于 Kotlin 中的 'Long' 和 'Int'
【发布时间】:2017-09-29 00:02:34
【问题描述】:

我正在尝试在 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
        }
    }

错误:

if (drawerItem.identifier == (1)

if (drawerItem.identifier == 2)

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

【问题讨论】:

  • 与 Java 不同,Kotlin 不会自动将数字提升为更广泛的类型。对于这些比较,您必须明确使用相同的类型。 Francesc 给出了正确答案,但如果你的 int 存储在变量中,你会做if (drawerItem.identifier == id.toLong())

标签: android kotlin operator-keyword


【解决方案1】:

只需在右侧使用 long

if (drawerItem.identifier == 1L)

编辑:这是必需的原因是 Kotlin 不会将 Ints 提升为 Longs(或者,更一般地说,不会扩大类型);在左侧,我们有一个 Long,在右侧,我们有一个 Int,这会导致错误。明确指出右侧是 Long 修复错误。

【讨论】:

  • L 显式转换将1 声明为Long 类型,因此现在可以比较它们。 “明确指出右侧是 Long 修复错误。”
  • “Kotlin 不提升/扩展类型”的说法在多大程度上是正确的?其他事情确实有效,例如有序比较和 elvis 运算符。例如,if (someLongValue &gt; 1) 有效(&gt;=&lt;&lt;=)有效,someNullableLongValue ?: 0 有效且类型为 Long
【解决方案2】:

出于兴趣,另一种解决方案是使用compareTo()compareTo 如果值相等则返回零,如果小于另一个则返回负数,如果大于另一个则返回正数:

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

【讨论】:

  • 不管怎样,测试 -1 或 1 与测试阴性或阳性不同。
【解决方案3】:

如果上述解决方案不适合您, 尝试显式将右侧的值转换为左侧的特定更宽类型

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

每种数字类型都支持以下转换:

  • toByte(): 字节
  • toShort():短
  • toInt(): 整数
  • toLong(): 长
  • toFloat(): 浮点数
  • toDouble(): 双倍
  • toChar(): 字符

【讨论】:

    【解决方案4】:

    如果您遇到以下问题 operator != 不适用于 long 和 int > 解决它只需在右侧使用 long 即价值!= 1L 就是这样。。

    【讨论】:

      【解决方案5】:

      确保当您将 Double 与 myDouble == 5 之类的任何数字进行比较时,在最后添加 .0 之前它不会起作用。这是因为 Kotlin 会根据输入的数字猜测类型,它会将 5 视为 int 并将 myDouble 视为 double 并产生相同的错误

      因此,始终明确地使比较变量与其他变量相同

      drawerItem.identifier == (1.0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 2022-06-11
        相关资源
        最近更新 更多