意外覆盖:以下声明具有相同的 JVM 签名 [英] Accidental override: The following declarations have the same JVM signature
问题描述
我在 Kotlin 的这一部分遇到错误:
class GitHubRepoAdapter(私有 val 上下文:上下文,私有 val 值:List) : ArrayAdapter(语境,R.layout.list_item,价值观)
私有val上下文:上下文
在日志中它说:
<块引用>Error:(14, 25) 意外覆盖:以下声明具有相同的 JVM 签名(getContext()Landroid/content/Context;):fun <get-context>(): 上下文有趣的 getContext():上下文!
我看不出是什么导致了问题.
发生这种情况是因为 Kotlin 编译器试图为在您的类主构造函数中声明的 val context
生成一个 getter,即一个方法 getContext()
,但是基类 ArrayAdapter
已经有这样的方法.
您可以通过执行以下操作之一来解决此问题:
将类的构造函数参数更改为不是
val
.class GitHubRepoAdapter(context: Context, ...
这种情况下不会生成getter,冲突也就消失了.
这似乎是您的首选解决方案,因为即使没有重新声明,已经有一个从 Java getter 推断出的综合属性
context
.使用
@JvmName
注释,应用到<代码>上下文代码>属性获取器:class GitHubRepoAdapter(@get:JvmName("getAdapterContext") private val context: Context, ...
这将使编译器生成具有另一个 JVM 名称(在注释中指定的名称)的 getter,从而避免冲突,但会使从 Java 访问它变得不那么直观(特别是因为会有两个相似的函数).在 Kotlin 中,您仍然可以使用原始名称
context
的属性.
I'm getting error in Kotlin in this part:
class GitHubRepoAdapter(
private val context: Context,
private val values: List<GithubRepo>
) : ArrayAdapter<GithubRepo>(
context,
R.layout.list_item,
values
)
private val context: Context
In the log it says:
Error:(14, 25) Accidental override: The following declarations have the same JVM signature (getContext()Landroid/content/Context;): fun <get-context>(): Context fun getContext(): Context!
I'm not able to see what is causing the problem.
This happens because the Kotlin compiler tries to generate a getter for val context
declared in your class primary constructor, namely a method getContext()
, but the base class ArrayAdapter<T>
already has such a method.
You can solve that by doing one of the following:
Change your class' constructor parameter not to be a
val
.class GitHubRepoAdapter(context: Context, ...
In this case, the getter won't be generated, and the conflict will be gone.
This seems to be the preferrable solution in your case, because, even without redeclaration, there is already a synthetic property
context
inferred from the Java getter.Use the
@JvmName
annotation, apply it to thecontext
property getter:class GitHubRepoAdapter(@get:JvmName("getAdapterContext") private val context: Context, ...
This will make the compiler generate the getter with another JVM name (the one specified in the annotation), thus avoiding the conflict, but making accessing it from Java less intuitive (especially since there will be two similar functions). In Kotlin, you will still be able to use the property with its original name
context
.
这篇关于意外覆盖:以下声明具有相同的 JVM 签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!