2015-04-12 61 views
2

我使用Kotlin创建了Android动态壁纸。这需要一个扩展WallpaperService的类,它包含一个扩展WallpaperService.Engine的内部类。继承内部java类的问题

所以我写了这一点:

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 

我无法弄清楚为什么会发生如此任何帮助将不胜感激。

回答

1

KT-6727

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

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

我试过这个,不幸的是它不起作用。 – user3307102

+0

这不适用于Android :( – carlospiles

+0

问题K-6727被标记为https://youtrack.jetbrains.com/issue/KT-3335的重复项,它已被标记为已修复。这仍然是问题,或者现在已解决? –

0

我已经找到了最好的解决方案是使用一个中间的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) { 
    } 
}