错误获取字体Landroid/content/Context;ILandroid/util/TypedValue。

10

我在我的项目中遇到了一个问题,不知道该如何解决。这是我第一次遇到这个问题。

/UncaughtException: java.lang.NoSuchMethodError: No static method getFont(Landroid/content/Context;ILandroid/util/TypedValue;ILandroid/widget/TextView;)Landroid/graphics/Typeface; in class Landroid/support/v4/content/res/ResourcesCompat; or its super classes (declaration of 'android.support.v4.content.res.ResourcesCompat' appears in /data/app/codes.trongtin.h.besttrip-2/split_lib_dependencies_apk.apk:classes2.dex)


1
可能是您正在使用错误的库版本。 - Indra Basak
我也遇到了同样的问题,今天我更新了我的Android Studio,从那时起就一直面临这个问题。 - Aditya Hari Kishan
在升级到支持库的v27版本后,我也遇到了同样的问题...进行一次清理构建就解决了我的问题。 - John O'Reilly
这个解决方案对我有用。点击这里。链接描述 - sidpal7747
6个回答

21

看起来你正在使用 support dependencies。 只需检查您的应用 gradle,确保您的构建工具版本与 support 版本相同。

例如:

android {
...
    buildToolsVersion "26.0.2"
...
}

dependencies {
...
    compile 'com.android.support:support-v4:26.0.2'
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:recyclerview-v7:26.0.2'
    compile 'com.android.support:support-v13:26.0.2'
...
}

必须具有相同的版本。

这对我有效!


6
只需更改 complieSdkVersion、buildToolsVersion 和 appCompat 依赖项即可。
      compileSdkVersion 27
      buildToolsVersion '27.0.0'

      implementation 'com.android.support:appcompat-v7:27.0.2'

3

感谢@agustin-ruiz-linares提供正确的答案。

我将在此处添加我的设置,以及我正在使用的Firebase和Glide库,以防对某人有所帮助。在我协调库版本以及buildToolsVersion和targetSdkVersions之前,我一直看到错误。

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 20
        targetSdkVersion 27
        buildToolsVersion "27.0.2"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:design:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-database:11.8.0'
    implementation 'com.google.firebase:firebase-storage:11.8.0'
    implementation 'com.firebaseui:firebase-ui-database:3.2.1'
    implementation 'com.github.bumptech.glide:glide:4.5.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
    implementation 'com.android.support:support-v4:27.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

1
这对我来说是正确的答案。26.0.2无法工作,但将每个依赖项移动到27.0.2中修复了所有错误。 - Miljan Rakita

1

如果任何支持依赖项的版本不匹配,则会出现此错误。

  1. 确保您正在使用最新版本的 compileSdkVersion 27
  2. 所有支持库的版本都应完全相同

0

由于您正在使用支持库,请使用ResourcesCompat

Typeface typeFace= ResourcesCompat.getFont(getActivity(),R.font.roboto);

这将解决你的问题。

如果你想支持26及以上的API级别,你可以使用

Typeface typeface = getResources().getFont(R.font.myfont);

对我来说,在xml布局中设置setContentView(R.layout.main)时出现了问题。如果我从布局中删除Textview,它就不会显示任何问题并且可以正常工作。但是我需要Textview,我该如何解决这个问题? - Aditya Hari Kishan
你正在使用 Android Studio 3.0 吗? - jomin v george
在我的情况下,我正在使用版本27.0.2。但问题仍然存在。 - noobEinstien
@Godwin 确保你正在使用 3.0 或更高版本的工作室。 - jomin v george

-1

解决方案非常简单...只需将构建工具版本更改为27.0.2,所有支持库也更改为27.0.2即可。

Done. Bingo!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接