Java – Android kotlin overrides the interface method in the onCreateView() method

Android kotlin overrides the interface method in the onCreateView() method… here is a solution to the problem.

Android kotlin overrides the interface method in the onCreateView() method

I’m new to Kotlin. I have an interface with two method definitions:

fun onSuccess(result: T)
fun onFailure(e: Exception)

Now, I’ve implemented this interface in my fragment and want to use these methods in it:

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
     ..................
     ..................
     override fun onSuccess(result: String) {}
     override fun onFailure(e: Exception) {}
}

In java we can use @override, but here I get the error message “modifier ‘override’ does not work for native functions”. I worked on kotlin for 2-3 days and I love it. But there are minor issues that require some time to debug.

Solution

You need to implement the interface on your fragment and move the override method outside of your onCreateView method.

MyFragment classes: fragment, MyInterface

You cannot override a method inside a method. Another option is that you can create an object expression like the following

window.addMouseListener(object : MouseAdapter() {
    override fun mouseClicked(e: MouseEvent) {
        // ...
    }

override fun mouseEntered(e: MouseEvent) {
        // ...
    }
})

https://kotlinlang.org/docs/reference/object-declarations.html

Related Problems and Solutions