继承内部java类的问题

我正在使用Kotlin创建一个Android动态壁纸。 这需要一个扩展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() { } } 

问题是我在编译时遇到以下两个错误:

 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 

我不知道为什么会发生这样的任何帮助将不胜感激。

参见KT-6727

您可以尝试以下解决方法:

 private inner class MyEngine : super.Engine() { } 

我发现的最好的解决方案是使用一个中间的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) { } }  public class MyWallpaperService : WallpaperService() { override fun onCreateEngine(): Engine = MyEngine()​ private inner class MyEngine : Intermediate(this) { } }