Tag: 科特林

委托给初始化属性时,“variables必须初始化”错误

object Foo : CharSequence by Foo.X { val X = “” } 产生 Variable ‘X’ must be initialized 但它是! 而代码应该翻译成类似的东西 object Foo : CharSequence { val X = “” override val length get() = Foo.X.length override operator fun get(index: Int): Char = Foo.X[index] override fun subSequence(startIndex: Int, endIndex: Int) = Foo.X.subSequence(startIndex, endIndex) } 这工作得很好。 这个错误的原因是什么,是否有解决方法? […]

kotlin-android null不能转换为非nulltypeskotlin.String

我无法同步我的项目,因为: Caused by: kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String at org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt.loadKotlinVersionFromResource(KotlinPluginWrapper.kt:89) at org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt.access$loadKotlinVersionFromResource(KotlinPluginWrapper.kt:1) at org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper.(KotlinPluginWrapper.kt:39) at org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper.(KotlinPluginWrapper.kt:70) at org.gradle.api.internal.DependencyInjectingInstantiator.newInstance(DependencyInjectingInstantiator.java:81) at org.gradle.api.internal.plugins.DefaultPluginManager.instantiatePlugin(DefaultPluginManager.java:72) … 126 more 我更新Kotlin插件到1.1.61后发生这种情况

Kotlingenerics和可空类types

如何处理Kotlin中的可空generics类? generics的示例函数: fun calculateStuff(valueType: Class, defaultValue: I): I { // do some work return defaultValue; } 这是一个调用函数(注意calculateStuff(…)的第二个参数) fun doStuff() { // works fine! val myVar1 = calculateStuff(String::class.java, “”) // FAIL (null is not accepted… Error: “Cannot infer type parameter I in….”) val myVar2 = calculateStuff(String::class.java, null) } 解决方法(将返回types更改为I?AND defaultValue为I): fun calculateStuff(valueType: Class, defaultValue: I?): I? […]

Kotlin与JPA / Hibernate:没有“开放”没有懒加载?

大多数Kotlin JPA示例代码看起来像这样 class Person(val name: String, val age: Int) { /* … */ } 甚至 data class Person(val name: String=””, val age: Int=0) { /* … */ } 现在,“ Hibernate用户指南”和其他几个ORM都指出,他们通常要创建代理或扩展模型类,但要允许在Kotlin中将类明确定义为open 。 这对于数据类来说目前是不可能的,从我自己的经验来看,大多数人在Kotlin编写JPA实体时并没有考虑这个问题。 所以,来我的问题(毕竟这是stackoverflow),是否足够 open class Person(val name: String, val age: Int) { /* … */ } 或者我们真的不得不这样做 open class Person(open val name: String, open val […]

Val不能在kotlin中重新分配编译时间错误的局部variables

这里在乐趣交换我试图改变a1与b1的值,但它显示val不能被重新分配编译时间错误。 如果我不能像这样改变,那么怎么可能呢。 fun swap(a1: String, b1: String) { val temp = a1 a1 = b1 b1 = temp } 注意:这只是一个示例,要知道为什么我不能像在java中那样重新分配局部variables

如何使IDE明白,方法调用后该对象完全不为null

我正在使用Kotlin语言的Android工作室 当我调用上面的函数,它的警告检查“obj”的空状态 如何使IDE(Android Studio)能够理解在某些方法调用之后,(传递的)对象完全不为空…谢谢

为什么Kotlin编译器需要var属性的显式初始化器?

我无法理解以下Kotlin文档: The initializer, getter and setter are optional. Property type is optional if it can be inferred from the initializer or from the base class member being overridden. Examples: var allByDefault: Int? // error: explicit initializer required, default getter and setter implied 为什么编译器需要显式初始化的唯一解释(至少是我能想到的唯一解释)是Kotlin没有默认的属性值。 这样对吗? 如果是这样,为什么? 换句话说:Kotlin属性和Java字段(具有默认值)之间的区别是什么?它们不允许我们使用属性的默认值?

界面的function与Property的getter冲突

一个接口的函数名与属性的getter名有意冲突,但由于意外覆盖问题,它被编译器禁止。 是否有可能指示编译器这是故意的? interface A { fun getFoo() } class B: A { val foo }

Gradle项目:java.lang.NoClassDefFoundError:kotlin / jvm / internal / Intrinsics

我正在开发一个Java项目,在这个项目中,我第一次尝试了Kotlin。 我开始使用Intellij Idea中提供的JavaToKoltin转换器将一些类转换为Kotlin。 其中我的自定义例外现在转换为Kotlin。 但是,这个exception处理不再正确工作了。 如果我在java代码中抛出一个自定义exception(例如MyCustomKotlinException.kt ),那么这个exception不会被捕获(见下面的代码)。 // Example.java package foo import java.util.*; import java.lang.*; import java.io.*; import foo.MyCustomKotlinException; class Example { public static void main (String[] args) { try { // Do some stuff // if Error MyCustomKotlinException e = new MyCustomKotlinException(“Error Message”); throw e; } catch (MyCustomKotlinException e) { // <– THIS PART […]

在kotlin中,如何传递一个MutableList,其中目标需要一个List

有一个定义为List值的哈希映射: private var mMap: HashMap<String, List>? = null 有一个函数返回一个哈希映射,但与MutableList的值 fun getDataStatus(response: JSONObject?): HashMap<String, MutableList> { return HashMap<String, MutableList>() } 当将结果传递给期望列表的HashMap时,会出现错误: mMap = getDataStatus(resp) //<== got error 有错误: Error:(81, 35) Type mismatch: inferred type is HashMap<String, MutableList> but HashMap<String, List>? was expected