You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin调用Java类出现‘unresolved reference’错误的解决咨询

解决Kotlin无法引用Java类Base32Decoder的问题

1. 修正变量名与类名冲突

你的代码里将变量名命名为Base32Decoder,和类名完全一致,这会导致Kotlin优先识别变量而非类。直接修改变量名即可:

fun main(args: Array<String>){
    val decoder = Base32Decoder()
    val decodedStr: String = decoder.base32Decode("JBSWY3DPFQQFO33SNRSCC===")
    println("inside the Kotlin codes:" + decodedStr)
}

2. 确保包导入正确

如果Base32Decoder是自定义Java类:

  • 若Kotlin文件和Java类在同一个包下,无需额外导入;
  • 若不在同一包,在Kotlin文件顶部添加包导入语句:
import 你的包路径.Base32Decoder // 替换为实际的包路径,比如com.example.utils.Base32Decoder

3. 检查Java类的可访问性

确保Java类是public修饰的,Kotlin无法访问Java中默认的包私有类。Java类定义需满足:

public class Base32Decoder {
    public String base32Decode(String input) {
        // 你的解码逻辑实现
        return "";
    }
}

4. 第三方库类的依赖检查

如果Base32Decoder是第三方库提供的(比如Apache Commons Codec中的Base32工具类):

  • 先确认项目已添加对应依赖,例如Maven项目在pom.xml中加入:
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>
  • 导入正确的类并调整调用方式(注意第三方类的方法名可能和你写的不一致):
import org.apache.commons.codec.binary.Base32

fun main(args: Array<String>){
    val decoder = Base32()
    val decodedStr = decoder.decodeAsString("JBSWY3DPFQQFO33SNRSCC===")
    println("inside the Kotlin codes:" + decodedStr)
}

内容的提问来源于stack exchange,提问作者EpicAdidash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 01:03:11