【问题标题】:addFontWeightStyle NoSuchMethodException on TextViewTextView 上的 addFontWeightStyle NoSuchMethodException
【发布时间】:2019-07-09 12:57:54
【问题描述】:

我正在为我的项目使用 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)

仍然出现此错误

【问题讨论】:

  • 我也遇到了同样的问题。
  • 我目前遇到错误,但它源于我对setContentView(R.layout.x)的调用

标签: android material-design androidx


【解决方案1】:

试试这个:

 <style name="CutsomFontTextView" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:fontFamily">@font/yourfont</item>
</style>

并将这个主题应用到如下文本视图

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/menu_tv_title"
    android:theme="@style/CutsomFontTextView"
    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"/>

【讨论】:

    【解决方案2】:

    您的样式父级是@android:style/TextAppearance.Small 点击那个。你只会看到两个项目

    <item name="textSize">14sp</item>
            <item name="textColor">?textColorSecondary</item>
    

    所以错误是正确的。该样式内部没有fontFamily 属性。

    尝试将您的父主题更改为

    <style name="font_work_sans_medium" parent="Base.Theme.AppCompat.Light.DarkActionBar">
     <item name="android:fontFamily">@font/work_sans_medium</item>
    </style>
    

    【讨论】:

      【解决方案3】:

      根据 Class.java 方法,当方法预期是公共的(或者如果不存在,我认为不是这种情况)并且不存在时,显然会发生这种情况(请看下面的评论):

      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;
      }
      

      我认为这是 Androidx 中的某种错误。

      【讨论】:

      • 有什么解决办法吗?
      • 好吧,如果我是正确的并且它是 AndroidX 中的一个错误,那就不是。我们将不得不等待更新。您可以尝试以编程方式加载字体。这可能会有所帮助。
      【解决方案4】:

      对于一些研究,我发现该解决方案可能对我有帮助,实际上我使用的是不稳定的 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'
      

      【讨论】:

      • 虽然@PerracoLabs提到了但是已经被举报了,版本降级是目前的解决方案。
      【解决方案5】:

      我从我的 xml 文件中删除了这一行,现在可以正常工作

      android:textStyle="bold"
      

      是关于 addFontWeightStyle Mehtod 在 API21 Impl 中抛出 NoSuchMethodException

      【讨论】:

      • 我没用过textStyle
      • 这确实有效,但这不是解决方案,因为问题是我们确实想使用 fontStyle。
      【解决方案6】:

      该问题已报告给Android team,并且似乎是未来版本的修复:

      根据以下版本的评论 #25

      implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
      implementation 'androidx.core:core-ktx:1.1.0-alpha05'
      

      【讨论】:

        【解决方案7】:

        仔细阅读堆栈跟踪 - 这是 AndroidX 的问题。似乎androidx.core.graphics.TypefaceCompatApi21Impl() 试图通过反射从框架类android.graphics.FontFamily 的某个内部静态解析带有签名addFontWeightStyle(lass java.lang.String, int, boolean) 的方法......结果证明该方法不存在。我对androidx.appcompat.widget.AppCompatAutoCompleteTextView 有同样的问题。在 Google 修复它之前,我们无能为力。

        【讨论】:

          【解决方案8】:

          您是否尝试在以编程方式设置字体时删除最后一个参数?

          getTitleView().setTypeface(typeFace)
          

          而不是

          getTitleView().setTypeface(typeFace, Typeface.NORMAL)
          

          内部使用 fontStyle

          【讨论】:

          • 是的,我尝试使用此代码 getTitleView().setTypeface(typeFace) 但直到无法正常工作
          【解决方案9】:

          这里是编码新手,但如果您通过 Build/'Edit Libraries and Dependencies' 进入工具栏并点击 Suggestions,然后针对 androidx.appcompat:appcompat:XXX 点击 update,它将修复错误而不是在 build.gradle 文件中进行:)

          【讨论】:

            猜你喜欢
            • 2019-07-06
            • 2019-07-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-31
            • 2019-01-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多