Kotlin Foo :: class.java“未解析的参考:Java"错误 [英] Kotlin Foo::class.java "Unresolved Reference: Java" error
问题描述
我正在尝试将HomePage.class
的Java代码转换为Kotlin.我正在按照 Kotlin.org 上的说明进行操作:
I am trying to convert my Java code of HomePage.class
to Kotlin. I am following the instructions on Kotlin.org:
getClass()
要从对象中检索类型信息,我们使用javaClass 扩展属性.
To retrieve the type information from an object, we use the javaClass extension property.
val fooClass = foo.javaClass
代替Java的Foo.class
使用
Foo::class.java
.
Instead of Java’s Foo.class
use
Foo::class.java
.
val fooClass = Foo::class.java
我有一个名为HomePage的类,用于扩展AppCompatActivity(在Android中).我正在使用Android Studio.我尝试做HomePage::class.java
,但出现错误:Unresolved reference: java
I have a class called HomePage that extends AppCompatActivity (in Android). I am using Android Studio. I tried doing HomePage::class.java
and it has an error: Unresolved reference: java
我如何使它工作?
推荐答案
该问题很可能是您忘记了依赖反射库,这是Kotlin的反射功能所必需的.
The issue is most likely that you forgot to depend on the reflection libraries which were needed for the reflective functions of Kotlin.
在Java平台上,使用 反射功能作为单独的JAR文件分发 (kotlin-reflect.jar).这样做是为了减小所需的 不使用反射功能的应用程序的运行时库. 如果您确实使用反射,请确保已添加.jar文件 到项目的类路径.
On the Java platform, the runtime component required for using the reflection features is distributed as a separate JAR file (kotlin-reflect.jar). This is done to reduce the required size of the runtime library for applications that do not use reflection features. If you do use reflection, please make sure that the .jar file is added to the classpath of your project.
这篇关于Kotlin Foo :: class.java“未解析的参考:Java"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!