I have an edit text which set some typeface spans with CustomTypefaceSpan which extends from TypefaceSpan and now I want to fetch spans from edit text by method getSpans and convert them to json object for the purpose of store in my database by using a method like this:
private fun typefaceSpanToJsonObject (editable: Editable): String?{
val typefaceSpans = editable.getSpans(
0,
editable.length,
CustomTypefaceSpan::class.java
)
val typefaceArrayList = ArrayList<MyTypefaceSpan>()
if (typefaceSpans != null) {
for (item in typefaceSpans){
val typeface: TypefaceName? = typefaceToTypefaceName(item.typeface!!)
if (typeface != null) {
typefaceArrayList.add(
MyTypefaceSpan(
typeface.toString(),
editable.getSpanStart(item),
editable.getSpanEnd(item)
)
)
}
}
}
return gson.toJson(typefaceArrayList)
}
unfortunately after run I encounter to this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kateb_realm_word_editor, PID: 15269
java.lang.NoSuchMethodError: No virtual method getTypeface()Landroid/graphics/Typeface; in class Landroid/text/style/TypefaceSpan; or its super classes (declaration of 'android.text.style.TypefaceSpan' appears in /system/framework/framework.jar:classes2.dex)
at com.example.kateb_realm_word_editor.Controller.Fragments.ViewLetterFragment.typefaceSpanToJsonObject(ViewLetterFragment.kt:746)
at com.example.kateb_realm_word_editor.Controller.Fragments.ViewLetterFragment.saveLetterWithSpansIntoDatabase(ViewLetterFragment.kt:647)
at com.example.kateb_realm_word_editor.Controller.Fragments.ViewLetterFragment.access$saveLetterWithSpansIntoDatabase(ViewLetterFragment.kt:48)
at com.example.kateb_realm_word_editor.Controller.Fragments.ViewLetterFragment$onViewCreated$9.onClick(ViewLetterFragment.kt:214)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22473)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
this is my CustomTypefaceSpan class :
import android.graphics.Paint
import android.graphics.Typeface
import android.text.TextPaint
import android.text.style.TypefaceSpan
class CustomTypefaceSpan(family: String, private val newType: Typeface) : TypefaceSpan(family) {
override fun updateDrawState(ds: TextPaint) {
applyCustomTypeFace(ds, newType)
}
override fun updateMeasureState(paint: TextPaint) {
applyCustomTypeFace(paint, newType)
}
companion object {
private fun applyCustomTypeFace(paint: Paint?, tf: Typeface) {
val oldStyle: Int
val old: Typeface = paint?.typeface ?: tf
oldStyle = old.style
val fake = oldStyle and tf.style.inv()
if (fake and Typeface.BOLD != 0 && paint != null) {
paint.isFakeBoldText = true
}
if (fake and Typeface.ITALIC != 0 && paint != null) {
paint.textSkewX = -0.25f
}
if (paint != null) {
paint.typeface = tf
}
}
}
}
what should I do? Thanks in advance for guidance.
Are you sure you are running app on Api 28 or above ? because item.typeface which means getTypeface() in Java that is available in API 28 or above
https://developer.android.com/reference/android/graphics/Typeface
https://developer.android.com/reference/android/text/style/TypefaceSpan#getTypeface()