Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addFontWeightStyle NoSuchMethodException on TextView

I am using Androidx library for my project, And I want to set a font to textview, so when I am applying any font to any Textview components than system gives me

TypefaceCompatApi21Impl: java.lang.NoSuchMethodException java.lang.NoSuchMethodException: addFontWeightStyle [class java.lang.String, int, boolean]

this type of error in run time but app not getting crash.

So how to overcome this error.

Note: It will properly work on without android x dependency.

Below my code:

<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/menu_tv_title"
        style="@style/font_work_sans_medium"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:maxLines="1"
        android:ellipsize="end"
        android:paddingEnd="@dimen/_12sdp"
        android:paddingStart="@dimen/_12sdp"
        android:textColor="@android:color/black"
        android:textSize="17sp"
        android:gravity="center"
        tools:text="title"
        tools:visibility="gone"/>

Here is the style

<style name="font_work_sans_medium" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/work_sans_medium</item>
</style>

I am also setting font by programatically like this

var typeFace: Typeface? = ResourcesCompat.getFont(context, R.font.work_sans_bold)
    getTitleView().setTypeface(typeFace, Typeface.NORMAL)

still getting this error

like image 753
Ankit Dubariya Avatar asked Feb 15 '19 11:02

Ankit Dubariya


3 Answers

For some research i found the solution might me helpful, actually i am using alpha dependency which is not stable so i downgrade the lib version of AndroidX

I am using this dependency

 implementation 'androidx.core:core-ktx:1.1.0-alpha04'
 implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'

you should use this instead of

implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.appcompat:appcompat:1.0.2'
like image 154
Ankit Dubariya Avatar answered Nov 10 '22 18:11

Ankit Dubariya


The issue has been reported to the Android team and seems to be a fix in place for a future release:

As per comment #25 below version works

implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.core:core-ktx:1.1.0-alpha05'
like image 22
PerracoLabs Avatar answered Nov 10 '22 18:11

PerracoLabs


I delete this line from my xml file and now works without errors

android:textStyle="bold"

is about addFontWeightStyle Mehtod which throw a NoSuchMethodException in API21 Impl

like image 8
Java Avatar answered Nov 10 '22 19:11

Java