Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

kotlin - Accidental override: The following declarations have the same JVM signature

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 the context 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...