从Java使用Kotlin单例 [英] Using Kotlin singleton from Java

查看:123
本文介绍了从Java使用Kotlin单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了所有内容,并且看到的所有内容都表明我应该能够做到这一点,因此肯定有一些我想念的小东西.我已经将Java类转换为Kotlin:

I've read up on this and everything I'm seeing says I should be able to do this so there must be some little thing I'm missing. I've converted a Java class to Kotlin:

object OrderTitle {
  @JvmOverloads
    fun generateMessage(context: Activity, otherParameter: AType? = null): AnotherType {
        // Do some things
   }
}

我从Java调用它:

message = OrderTitle.generateMessage(activity, property);

并收到此错误:

error: non-static method generateMessage(Activity,Property) cannot be referenced from a static context

推荐答案

@JvmStatic 注释函数,以便在编译时生成真正的静态Java函数.

Annotate your function with @JvmStatic so a real static java function is generated when compiling.

这篇关于从Java使用Kotlin单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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