Sudden "overrides nothing" errors appear, no recent change to Kotlin build version


#1

Just today I am trying to build an app module in Android Studio, with Kotlin 1.1.51. This code to implement PagerAdapter has compiled for a long time, but suddenly it is getting confused with overriding function parameter and/or return types of Java interface.

    internal inner class RegistrationPagerAdapter : PagerAdapter(), ViewPager.OnPageChangeListener {
    var expanded: Boolean = false
        set(value) {
            if (field == value)
                return
            field = value
            notifyDataSetChanged()
            this@LoginActivity.pageIndicator.notifyDataSetChanged()
        }
    val layouts = arrayOf(R.layout.fragment_login, R.layout.fragment_reg_1, R.layout.fragment_reg_2,
                          R.layout.fragment_reg_3, R.layout.fragment_reg_4)

    override fun instantiateItem(container: ViewGroup, position: Int): ViewDataBinding? {
        val layout = layouts[position]
        return when (layout) {
            R.layout.fragment_login -> this@LoginActivity.setupLogin(container)
            R.layout.fragment_reg_1 -> this@LoginActivity.setupPage1(container)
            R.layout.fragment_reg_2 -> this@LoginActivity.setupPage2(container)
            R.layout.fragment_reg_3 -> this@LoginActivity.setupPage3(container)
            R.layout.fragment_reg_4 -> this@LoginActivity.setupPage4(container)
            else -> throw IllegalStateException("unknown item in RegistrationPagerAdapter")
        }
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        when (layouts[position]) {
            R.layout.fragment_login -> this@LoginActivity.loginBinding = null
            R.layout.fragment_reg_1 -> this@LoginActivity.reg1Binding = null
            R.layout.fragment_reg_2 -> this@LoginActivity.reg2Binding = null
            R.layout.fragment_reg_3 -> this@LoginActivity.reg3Binding = null
            R.layout.fragment_reg_4 -> this@LoginActivity.reg4Binding = null
        }
        container.removeView((`object` as ViewDataBinding).root)
    }

    override fun isViewFromObject(view: View?, `object`: Any): Boolean {
        if (`object` === this@LoginActivity.loginBinding)
            return view === this@LoginActivity.loginBinding?.root
        if (`object` === this@LoginActivity.reg1Binding)
            return view === this@LoginActivity.reg1Binding?.root
        if (`object` === this@LoginActivity.reg2Binding)
            return view === this@LoginActivity.reg2Binding?.root
        if (`object` === this@LoginActivity.reg3Binding)
            return view === this@LoginActivity.reg3Binding?.root
        if (`object` === this@LoginActivity.reg4Binding)
            return view === this@LoginActivity.reg4Binding?.root

        throw IllegalStateException("unknown item in RegistrationPagerAdapter")
    }
    override fun restoreState(state: Parcelable, loader: ClassLoader?) {
        this.expanded = Parcels.unwrap(state)
        super.restoreState(state, loader)
    }
...

And now, I suddenly have errors

e: (1072, 20): Class ‘RegistrationPagerAdapter’ is not abstract and does not implement abstract base class member public abstract fun isViewFromObject(@NonNull p0: View, @NonNull p1: Any): Boolean defined in android.support.v4.view.PagerAdapter
e: (1085, 76): Return type of ‘instantiateItem’ is not a subtype of the return type of the overridden member '@NonNull public open fun instantiateItem(@NonNull p0: ViewGroup, p1: Int): Any defined in android.support.v4.view.PagerAdapter’
e: (1108, 9): ‘isViewFromObject’ overrides nothing
e: (1155, 9): ‘restoreState’ overrides nothing

The IDE doesn’t show an error in the open source file. I really can’t imagine why these would suddenly start to appear. IDE has Kotlin plugin of same version as dependency in build.gradle


#2

Just remove nullable ‘?’ from you arguments on overrided function

1- override fun isViewFromObject(view: View?, object: Any): Boolean {}

2- override fun restoreState(state: Parcelable, loader: ClassLoader?) {}

3- override fun instantiateItem(container: ViewGroup, position: Int): ViewDataBinding? {}

To

1- override fun isViewFromObject(view: View, object: Any): Boolean {}

2- override fun restoreState(state: Parcelable, loader: ClassLoader) {}

3- override fun instantiateItem(container: ViewGroup, position: Int): ViewDataBinding {}


#3

Did you perhaps update the android/support library version? Google has been adding annotations, also for Kotlin compatibility. This has probably interfered with your code.


#4

Thanks, I think that was it. Though strangely I have done full builds since changing library versions! I don’t understand why the editor thinks the code is fine. Bug in analyzers?


#5

maybe you haven’t updated your analyzer version yet? For me the analyzer version was still set to Kotlin 1.1 and then i wondered why isInitialized for a lateinit variable wouldn’t work…

Something similar may be happening to you