Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 运算符==不能应用于';长';和';Int';在科特林_Android_Kotlin_Operator Keyword - Fatal编程技术网

Android 运算符==不能应用于';长';和';Int';在科特林

Android 运算符==不能应用于';长';和';Int';在科特林,android,kotlin,operator-keyword,Android,Kotlin,Operator Keyword,我正试图在Kotlin中实现Mike Penz的NavigationDrawer()的一部分。从那以后,我只遇到了几个问题,主要是关于操作员的问题。下面是示例抽屉本身的部分代码。Android Studio不会抛出任何错误,除非我在int和Long变量上使用==运算符: // Create the Drawer result = DrawerBuilder() .withSliderBackgroundColor(ContextCo

我正试图在Kotlin中实现Mike Penz的NavigationDrawer()的一部分。从那以后,我只遇到了几个问题,主要是关于操作员的问题。下面是示例抽屉本身的部分代码。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
        }
    }
//创建抽屉
结果=抽屉生成器()
.withSliderBackgroundColor(ContextCompat.getColor(applicationContext,R.color.top_标题))
.withActivity(本)
.withToolbar(工具栏)
.WithHassTableId(真)
.withItemAnimator(AlphaCrossFadeAnimator())
.withAccountHeader(headerResult!!)
艾德瑞特姆斯先生(
PrimaryDrawerItem()。withName(R.string.drawer\u item\u profile)。withIcon(FontAwesome.Icon.faw\u user)。withIdentifier(1)。withSelectable(false)。withIconColor(ContextCompat.getColor(applicationContext,R.color.Icon\u grey))。withTextColor(ContextCompat.getColor(ApplicationContextContext,R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer\u item\u create)。withIcon(FontAwesome.Icon.faw\u paint\u brush)。withIdentifier(2)。withSelectable(false)。withIconColor(ContextCompat.getColor(applicationContext,R.color.Icon\u grey))。withTextColor(ContextCompat.getColor(ApplicationContextContext,R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer\u item\u yaanich\u news)。withIcon(FontAwesome.Icon.faw\u Paper\u o)。withIdentifier(3)。withSelectable(false)。withIconColor(ContextCompat.getColor(applicationContext,R.color.Icon\u grey))。withTextColor(ContextCompat.getColor(ApplicationContextContextContext,R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer\u item\u my\u groups)。withIcon(FontAwesome.Icon.faw\u users)。withIdentifier(4)。withSelectable(false)。withIconColor(ContextCompat.getColor(applicationContext,R.color.Icon\u grey))。withTextColor(ContextCompat.getColor(ApplicationContextContext,R.color.stroke)),
PrimaryDrawerItem().withName(R.string.drawer\u item\u settings)。withIcon(FontAwesome.Icon.faw\u cog)。withIdentifier(5)。withSelectable(false)。withIconColor(ContextCompat.getColor(applicationContext,R.color.Icon\u grey))。withTextColor(ContextCompat.getColor(ApplicationContextContext,R.color.stroke))
)
.withOnDrawerItemClickListener{视图、位置、抽屉属性->
if(drawerItem!=null){
变量intent:intent?=null
if(m.identifier==(1){
intent=intent(这是UserProfileActivity::class.java)
}else if(drawerItem.identifier==2){
intent=intent(这是YetActivity::class.java)
}else if(drawerItem.identifier==3){
intent=intent(这是RssActivity::class.java)
}else if(drawerItem.identifier==4){
intent=intent(这是GroupsActivity::class.java)
}else if(drawerItem.identifier==5){
intent=intent(这是UserSettingsActivity::class.java)
}
if(intent!=null){
这就是我们的目标
}
}
假的
}
.带有SavedInstance(savedInstanceState)
.WithShowDroneRonFirstLaunch(真实)
.build()
RecyclerViewCacheUtil().withCacheSize(2).apply(结果!!.recyclerView,结果!!.drawerItems)
如果(savedInstanceState==null){
结果!!.setSelection(21,false)
headerResult!!.activeProfile=profile
}
}
错误:

if(drawerItem.identifier==(1)

if(drawerItem.identifier==2)


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

只需在右侧使用Long即可

if (drawerItem.identifier == 1L)
编辑:之所以需要这样做,是因为Kotlin不将Int升级为Long(或者更一般地说,不加宽类型);左侧是Long,右侧是Int,这导致了错误。明确指出右侧是Long可以修复错误。

如果您面临 <运算符!=未应用于long和int 只需在右侧使用long即可解决此问题 ie值!=1L
就是这样。

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

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

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

num = 5.7
if (num == 4.toDouble()) {
    //some code
}
每种数字类型都支持以下转换:

  • toByte():字节
  • toShort():Short
  • toInt():Int
  • toLong():长
  • toFloat():浮动
  • toDouble():Double
  • toChar():Char

确保当你与MyPodi==5的任意数比较时,直到你在末尾加0。它是不起作用的。这是因为Kotlin从输入的数字中猜出类型,它会考虑5作为int和Mydouble为double,并且会产生相同的错误

。 因此,始终显式地使比较变量与其他变量相同

drawerItem.identifier == (1.0)

与Java不同,Kotlin不会自动将数字升级为更广泛的类型。您必须显式使用相同的类型进行这些比较。Franscesc给出了正确的答案,但如果您的int存储在变量中,您将执行
if(drawerItem.identifier==id.toLong())
。值得一提的是,测试-1或1与测试负或正不一样。L显式强制转换声明