Home > front end >  How to override method in library?
How to override method in library?

Time:09-07

That's my gradle with library

api 'com.simplify:ink:1.0.2'

In the library file, which is read-only Inkview.java This method is triggered, when every popup, showkeyboard is displayed.

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        clear();
    }

I would like to override it to just not call this method, or call it whether I need it. I have created InkView.kt

fun InkView.onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {

}

but it is triggered by default anyway. How to apply this proper?

EDIT: After first post I have created new class MyInkView and add it to .xml file.

    <com.myApplication.fragment.extensions.MyInkView
        android:id="@ id/inkView"
        android:layout_margin="@dimen/padding"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

error when enter to the fragment:

Error inflating class com.myApplication.fragment.extensions.MyInkView
    Caused by: java.lang.NoSuchMethodException: com.myApplication.fragment.extensions.MyInkView.<init> [class android.content.Context, interface android.util.AttributeSet]

If I add attributeSet as null then error is:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference

Second Edit:

class MyInkView: InkView {
    constructor(context: Context, attrs: AttributeSet): super(context, attrs)
    constructor(context: Context): super(context)
    // include any other constructors you need based on the ones in the superclass

    fun InkView.onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        // your code here
    }
}

Doesn't override anything, and doesn't give crash. It works like before.

class MyInkView: InkView {
    constructor(context: Context, attrs: AttributeSet): super(context, attrs)
    constructor(context: Context): super(context)
    // include any other constructors you need based on the ones in the superclass

   override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        // your code here
    }
}

error log:

    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference

clear method from inkView

// clean up existing bitmap
       if (bitmap != null) {
           bitmap.recycle()
       }

       // init bitmap cache
       this.bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
       canvas = Canvas(bitmap)

       // notify listeners
       for (listener in listeners) {
           listener.onInkClear()
       }

       invalidate()
       isEmpty = true

    }

CodePudding user response:

Kotlin extension functions are statically resolved, and can't override member functions.

If you want to override a member function of InkView, you'll need to create a new class that extends from it.

For example:

class MyInkView(context: Context): InkView(context) {
    override fun InkView.onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        // your code here
    }
}

To allow Android to instantiate the class properly, you probably also need the additional two-arg constructor:

class MyInkView: InkView {
    constructor(context: Context, attrs: AttributeSet): super(context, attrs)
    constructor(context: Context): super(context)
    // include any other constructors you need based on the ones in the superclass

    override fun InkView.onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        // your code here
    }
}
  • Related