when studio que lazy lateinit espaƱol array android kotlin operator-keyword

android - studio - lateinit kotlin que es



El operador== no se puede aplicar a ''Largo'' e ''Int'' en Kotlin (3)

Estoy intentando implementar partes de NavigationDrawer de Mike Penz ( https://github.com/mikepenz/MaterialDrawer ) en Kotlin. Desde entonces me he encontrado con solo algunos problemas, principalmente con los operadores. Aquí está parte del código para crear una instancia del propio cajón. Android Studio no genera ningún error, excepto cuando estoy usando el operador == en las variables int y 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 } }

Errores:

if (drawerItem.identifier == (1)

if (drawerItem.identifier == 2)

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


Para interés, otra solución sería usar compareTo() . compareTo devuelve cero si los valores son iguales, negativo si es menor que el otro, o positivo si es mayor que el otro:

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


Simplemente use largo en su lado derecho

if (drawerItem.identifier == 1L)

Edición: la razón por la que se requiere esto es que Kotlin no promueve Ints a Longs (o, más generalmente, no amplía los tipos); en el lado izquierdo teníamos un Largo y en el lado derecho teníamos un Int, que nos llevó al error. Indica explícitamente que el lado derecho es un error Largo arregla el error.


si está enfrentando el problema de < operator! = no se aplica a long y int > resuélvalo Simplemente usando long en su lado derecho, es decir, value! = 1L eso es todo ...