我正在使用 Kotlin 创建一个安卓动态壁纸。这需要一个扩展 WallpaperService 的类,它包含一个扩展 WallpaperService.Engine 的内部类。
所以我写了这个:
import android.service.wallpaper.WallpaperService
import android.service.wallpaper.WallpaperService.Engine
public class MyWallpaperService : WallpaperService() {
override fun onCreateEngine(): Engine = MyEngine()
private inner class MyEngine : Engine() {
}
}
问题是我在编译时遇到以下 2 个错误:
Error:java.lang.RuntimeException: Error generating constructors of class MyEngine with kind IMPLEMENTATION
Error:java.lang.UnsupportedOperationException: Don't know how to generate outer expression for lazy class MyWallpaperService
我无法弄清楚为什么会发生这种情况,因此非常感谢任何帮助。
最佳答案
我发现的最佳解决方案是使用中间 Java 类:
public class Intermediate extends WallpaperService.Engine {
public Intermediate(WatchfaceService outer) {
outer.super();
}
}
那么你的 Kotlin WallpaperService 中的内部类应该继承 Intermediate,将外部类作为参数传递。
public class MyWallpaperService : WallpaperService() {
override fun onCreateEngine(): Engine = MyEngine()
private inner class MyEngine : Intermediate(this) {
}
}
关于android - 继承内部java类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596016/