我如何能够从 kotlin 类中的 java Singleton 类获取数据?由于我的项目是一个旧项目,整个应用程序都在java中,现在我正在kotlin中实现它的新模块,其中我在kotlin的服务类(中使用Java的单例
。当我使用此 (CurrentUser.java)
( UpdateWidgetService.kt)CurrentUser.getInstance().userType
代码来获取我的服务类中的用户类型时,它会抛出异常
Process: com.example.myapp, PID: 27066
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/myapp/main/CurrentUser
Java
public class CurrentUser {
String userType = "";
private static CurrentUser instance;
public static CurrentUser getInstance() {
if (instance == null) {
instance = new CurrentUser();
}
return instance;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getUserType() {
return userType ;
}
}
Kotlin
CurrentUser.getInstance().userType
最佳答案
重建并无效缓存您的项目,然后重新启动您的 android studio。 还同步文件系统。 我遇到了同样的问题,我做了这个技巧,它对我有用。 希望这可以帮助您。
关于java - 在 kotlin 类中调用 Java 单例类时出现 NoClassDefFound 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081016/