私はKotlinにマイクペンツのNavigationDrawer(https://github.com/mikepenz/MaterialDrawer)の部分を実装しようとしています。それ以来、主にオペレーターを中心にいくつかの問題に取り組んできました。ここでは、引き出し自体をインスタンス化するコードの一部です。オペレーター==はコットリンの 'Long'と 'Int'には適用できません
// 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'
のAndroid Studioは、私はintとlong変数の==演算子を使用している場合を除き、すべてのエラーをスローしません。
Javaとは異なり、Kotlinは自動的に番号をより広範なタイプに昇格しません。これらの比較には、同じタイプを明示的に使用する必要があります。 Franscescは正しい答えを返しましたが、あなたのintが変数に格納されていれば 'if(drawerItem.identifier == id.toLong())'を実行します。 –