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

标签 android kotlin operator-keyword

我正在尝试在 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 中将日期字符串转换为所需的格式

C++ 重载运算符问题

c++ - 调用构造函数的赋值运算符被破坏

android - 如何使用 pjsua2 android 发送 SIP 消息

java - Android Studio 无法启动 Activity ComponentInfo

kotlin - 如何修复我的代码以删除强制转换警告?

javascript如果条件&&条件不起作用

android - 在 Fragment 中保存状态 WebView

c++ - Android设备 radio ROM编程

android - 在通话期间显示交互式 float 布局