Kotlin unresolved reference in IntelliJ

后端 未结 20 1297
太阳男子
太阳男子 2020-12-01 08:47

I started off with the tutorial for learning Kotlin in IntelliJ.When I tried running the example i.e

fun main(args: Array) {
 prin         


        
相关标签:
20条回答
  • 2020-12-01 09:11

    Sometimes in similar situations (I don't think it is your problem because your case is very simple) it's worth to check kotlin file package.

    If you have Kotlin file within the same package and put there some classes and missed the package declaration it looks inside the IntelliJ that you have classes in the same package but without definition of package the IntelliJ shows you:

    Error:(5, 5) Kotlin: Unresolved reference: ...

    0 讨论(0)
  • 2020-12-01 09:12

    I had this issue because the linter was printing that my object was an instance of Foo whereas the compiler couldn't define its type and consider it was an Any object like (It was more complex in my case, the linter show error in below code but the idea is here) :

        class Foo {
            val bar: Int = 0
        }
    
        fun test(): Any {
            return Foo()
        }
    
        val foo = test()
        foo.bar // <-- Linter consider that foo is an Instance of Foo but not the compiler because it's an Any object so it show the error at compilation.
    

    To fix it I had to cast my object :

    val foo = test() as Foo
    
    0 讨论(0)
  • 2020-12-01 09:15

    Ran into this issue. I had to add the following to my build.grade:

    apply plugin: 'kotlin-android'

    0 讨论(0)
  • 2020-12-01 09:16

    A possible solution for standalone Kotlin projects is to include Kotlin standard libs explicitliy inside the root project.

    To do that in IntelliJ IDEA:

    • press Ctrl+Shift+A (Search actions or options)
    • type in "Configure kotlin in project" and let it include standard libs for you
    0 讨论(0)
  • 2020-12-01 09:16

    Happened to me today.

    My issue was that I had too many tabs open (I didn't know they were open) with source code on them.

    If you close all the tabs, maybe you will unconfuse IntelliJ into indexing the dependencies correctly

    0 讨论(0)
  • 2020-12-01 09:18

    Had the same with IDEA 14.1.5, Kotlin v.1.0.0-beta-1038-IJ141-17.
    Kotlin gets its own list entry (like Java) when creating new project, but the only working config was:

    New | Project | Java | "Kotlin (Java)" (make sure you have Project SDK configured, too)
    use library: Create, "Copy to: lib".

    0 讨论(0)
提交回复
热议问题