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

我正在尝试在 Kotlin 中实现 Mike Penz 的 NavigationDrawer (https://github.com/mikepenz/MaterialDrawer) 的部分内容。从那以后,我只遇到了几个问题,主要是运算符(operator)。这是实例化抽屉本身的部分代码。 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)

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

最佳答案

只需在右侧使用 long

if (drawerItem.identifier == 1L)

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

关于android - 运算符 == 不能应用于 Kotlin 中的 'Long' 和 'Int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43721362/

相关文章:

java - 如何从 Kotlin/Java 中运行 Kotlin-Script (.kts) 文件

java - Gradle 项目 : java. lang.NoClassDefFoundError

android - 将项目添加到 LiveData 列表时通知观察者

kotlin - Kotlin 是 "pass-by-value"还是 "pass-by-refer

regex - 使用正则表达式在 Kotlin 中进行 URL 解析

android - 如何使用 Kotlin 设置 OnEditorActionListener

android - 如何在 RecyclerView.ViewHolder 中使用 kotlin 绑

android - 实时模板在 Kotlin 中不起作用

Kotlin - 等待函数

java - Kotlin 数据类实现 Java 接口(interface)