android - TextView 上的 addFontWeightStyle NoSuchMethodException

标签 android material-design androidx

我正在为我的项目使用 Androidx 库,并且我想将 font 设置为 textview,所以当我应用任何字体时除了系统给我的任何 Textview 组件

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

这种类型的运行时错误,但应用程序没有崩溃。

那么如何克服这个错误。

注意:它可以在没有 android x 依赖的情况下正常工作。

在我的代码下面:

<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"/>

这里是风格

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

我也像这样以编程方式设置字体

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

仍然出现此错误

最佳答案

对于一些研究,我发现解决方案可能对我有帮助,实际上我使用的是不稳定的 alpha 依赖项,因此我降级了 AndroidX 的 lib 版本

我正在使用这个依赖

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

你应该用这个代替

implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.appcompat:appcompat:1.0.2'

关于android - TextView 上的 addFontWeightStyle NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54708366/

相关文章:

java - 如何禁用向自身发送好友请求的按钮?

Javascript/AngularJS 要求消费者下载 iOS/Android 应用程序

android - 扩展 Activity 或 ActionBarActivity

android - 在 Android 上使用背景

Android 设计支持库和 Material Design 向后兼容?

尝试构建 React Native 0.59 应用程序时与 AndroidX 相关的 AndroidManifest 错误

java - 当我尝试子类化 ServiceConnection 时得到 "no interface expected here"

css - Material ui 中的波纹影响用不同的颜色填充按钮

android - 播放服务广告与 appcompat 冲突

java - list 合并失败 : Attribute application@appComponentFactory updating Firebase libraries