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
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'
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'
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
Just read the stack trace carefully - it's an issue with AndroidX. It seems that androidx.core.graphics.TypefaceCompatApi21Impl()
attempts to resolve statically a method with signature addFontWeightStyle(lass java.lang.String, int, boolean)
from some internal to the framework class android.graphics.FontFamily
via reflection... and it turns out that method doesn't exist. I am having the same issue with androidx.appcompat.widget.AppCompatAutoCompleteTextView
. Not much we can do until Google fixes it.
Apparently it is happening when the method expected to be public (or if not existing, which I believe is not the case) and is not, according to Class.java method (look at the comment below):
private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods)
throws NoSuchMethodException {
if (name == null) {
throw new NullPointerException("name == null");
}
if (parameterTypes == null) {
parameterTypes = EmptyArray.CLASS;
}
for (Class<?> c : parameterTypes) {
if (c == null) {
throw new NoSuchMethodException("parameter type is null");
}
}
Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes)
: getDeclaredMethodInternal(name, parameterTypes);
***// Fail if we didn't find the method or it was expected to be public.***
if (result == null ||
(recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) {
throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes));
}
return result;
}
I believe it is some kind of a bug in Androidx.