Home » Android » java – Error get Font Landroid/content/Context;ILandroid/util/TypedValue

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

Posted by: admin May 14, 2020 Leave a comment

Questions:

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)

How to&Answers:

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

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!

Answer:

Just Change complieSdkVersion, buildToolsVersion and appCompat dependency.

      compileSdkVersion 27
      buildToolsVersion '27.0.0'

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

Answer:

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

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.

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

Answer:

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

  1. Make sure you are using the Latest version of compileSdkVersion 27
  2. ALL the support libraries should be of exactly same version

Answer:

Since you are using support library, Use 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

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

Answer:

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

Done.
Bingo!