可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)
回答1:
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!
回答2:
Just Change complieSdkVersion, buildToolsVersion and appCompat dependency.
compileSdkVersion 27 buildToolsVersion '27.0.0' implementation 'com.android.support:appcompat-v7:27.0.2'
回答3:
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' }
回答4:
This error happen if there is a mismatch in version of any support dependency . 2 things to consider to avoid this error:
- Make sure you are using the Latest version of compileSdkVersion 27
- ALL the support libraries should be of exactly same version
回答5:
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);
回答6:
The solution is very simple ..just change build tool version to 27.0.2 and all supports library to 27.0.2
Done. Bingo!