Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems inheriting inner java class

Tags:

android

kotlin

I'm creating an android live wallpaper using Kotlin. This requires a class that extends WallpaperService, which contains an inner class that extends WallpaperService.Engine.

So I've written this:

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() {

    }
}

The problem is that I'm getting the following 2 errors at compile time:

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

I cant figure out why this is happening so any help would be greatly appreciated.

like image 569
user3307102 Avatar asked Sep 13 '26 06:09

user3307102


1 Answers

The best solution I have found is to use an intermediate Java class:

public class Intermediate extends WallpaperService.Engine {
    public Intermediate(WatchfaceService outer) {
        outer.super();
    }
}

Then the inner class in your Kotlin WallpaperService should inherit Intermediate, passing the outer class as parameter.

public class MyWallpaperService : WallpaperService() {
    override fun onCreateEngine(): Engine = MyEngine()
​
    private inner class MyEngine : Intermediate(this) {
    }
}
like image 185
carlospiles Avatar answered Sep 15 '26 20:09

carlospiles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!