意外覆盖:以下声明具有相同的JVM签名 [英] Accidental override: The following declarations have the same JVM signature

查看:941
本文介绍了意外覆盖:以下声明具有相同的JVM签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一部分,我在Kotlin中遇到此错误:

I'm getting this 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) {

私有val上下文:上下文

在日志中显示:

错误:(14,25)意外覆盖:以下声明具有 相同的JVM签名(getContext()Landroid/content/Context;): fun():上下文 fun getContext():上下文!

Error:(14, 25) Accidental override: The following declarations have the same JVM signature (getContext()Landroid/content/Context;): fun (): Context fun getContext(): Context!

我看不到是什么原因引起的.

I'm not able to see what is causing the problem.

推荐答案

之所以会发生这种情况,是因为Kotlin编译器尝试为您的类主构造函数中声明的val context生成吸气剂,即方法getContext(),但基类 ArrayAdapter<T>已经有这样的方法.

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:

  • 将类的构造函数参数更改为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.

使用 @JvmName 批注,将其应用于context属性获取器:

Use the @JvmName annotation, apply it to the context property getter:

 class GitHubRepoAdapter(@get:JvmName("getContext_") private val context: Context, ...

这将使编译器生成具有另一个JVM名称(在注释中指定的名称)的getter,从而避免了冲突,但使从Java访问它变得不那么直观(特别是因为将有两个类似的函数).在Kotlin中,您仍然可以使用其原始名称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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆