那是我與圖書館的畢業典禮
api 'com.simplify:ink:1.0.2'
在庫檔案中,是只讀的,Inkview.java
這個方法被觸發,每次彈出時,都會顯示showkeyboard。
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
clear();
}
我想重寫它以不呼叫此方法,或者在需要時呼叫它。我創造了InkView.kt
fun InkView.onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
}
但無論如何它都是默認觸發的。如何正確應用這個?
編輯:第一次發布后,我創建了新類 MyInkView 并將其添加到.xml檔案中。
<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 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]
如果我添加attributeSet為 null 那么錯誤是:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference
第二次編輯:
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
}
}
不會覆寫任何東西,也不會崩潰。它像以前一樣作業。
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
}
}
錯誤日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference
來自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
}
uj5u.com熱心網友回復:
Kotlin 擴展函式是靜態決議的,不能覆寫成員函式。
如果要覆寫 的成員函式InkView,則需要創建一個從它擴展的新類。
例如:
class MyInkView(context: Context): InkView(context) {
override fun InkView.onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
// your code here
}
}
為了讓 Android 能夠正確地實體化類,您可能還需要額外的兩個引數建構式:
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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/505023.html
