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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46945602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:25:38  来源:igfitidea点击:

Error get Font Landroid/content/Context;ILandroid/util/TypedValue

javaandroid

提问by Hu?nh Tr?ng Tín

I have problem with my project. I don't know how to fix it. This is first time I meet it.

我的项目有问题。我不知道如何修复它。这是我第一次遇到它。

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

/UncaughtException: java.lang.NoSuchMethodError: 没有静态方法 getFont(Landroid/content/Context;ILandroid/util/TypedValue;ILandroid/widget/TextView;)Landroid/graphics/Typeface; 在类 Landroid/support/v4/content/res/ResourcesCompat 中;或其超类('android.support.v4.content.res.ResourcesCompat' 的声明出现在 /data/app/codes.trongtin.h.besttrip-2/split_lib_dependencies_apk.apk:classes2.dex 中)

回答by Agustín Ruiz Linares

It seems you are using support dependencies. Just check in your app gradle that your build tools version is the same as support versions.

您似乎正在使用支持依赖项。只需在您的应用程序 gradle 中检查您的构建工具版本是否与支持版本相同。

For example:

例如:

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

and

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'
...
}

must have the same version.

必须有相同的版本。

It worked for me!

它对我有用!

回答by Dheeraj Rijhwani

Just Change complieSdkVersion, buildToolsVersion and appCompat dependency.

只需更改 complieSdkVersion、buildToolsVersion 和 appCompat 依赖项。

      compileSdkVersion 27
      buildToolsVersion '27.0.0'

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

回答by Lucy

Thanks @agustin-ruiz-linares for the correct answer.

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

I'll add my setup here along with the Firebase and Glide libraries that I'm using in case its helpful for someone. I was seeing the error until I reconciled the library versions along with the buildToolsVersion and targetSdkVersions, too.

我将在这里添加我的设置以及我正在使用的 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'
}

回答by Faheem

This error happen if there is a mismatch in version of any support dependency . 2 things to consider to avoid this error:

如果任何支持依赖项的版本不匹配,就会发生此错误。避免此错误需要考虑的 2 件事:

  1. Make sure you are using the Latest version of compileSdkVersion 27
  2. ALL the support libraries should be of exactly same version
  1. 确保您使用的是最新版本的 compileSdkVersion 27
  2. 所有支持库都应该是完全相同的版本

回答by jomin v george

Since you are using support library, Use ResourcesCompat

由于您使用的是支持库,请使用 ResourcesCompat

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

This will fix your problem.

这将解决您的问题。

If you want to support api level 26 and above you can use

如果你想支持 api level 26 及以上,你可以使用

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

回答by Mihir Joshi

The solution is very simple ..just change build tool version to 27.0.2 and all supports library to 27.0.2

解决方案很简单..只需将构建工具版本更改为 27.0.2 并且所有支持库都更改为 27.0.2

Done. Bingo!

完毕。答对了!