Android错误: java.lang.NoSuchMethodError: No static method getFont(Landroid/content/Context;...

Android Studio开发,时不时总是会碰到一些怪问题。比如使用Glide,大部分可能都碰到过这个错误,

E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.spacesoftwares.myapplication3, PID: 5768
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/ com.spacesoftwares.myapplication3-1/split_lib_dependencies_apk.apk)

  
      at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)
      at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208)
      at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:110)
      at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
      at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:81)
      at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
      at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:67)
      at android.support.v7.widget.Toolbar.setTitle(Toolbar.java:753)
      at android.support.v7.widget.ToolbarWidgetWrapper.setTitleInt(ToolbarWidgetWrapper.java:261)
      at android.support.v7.widget.ToolbarWidgetWrapper.setWindowTitle(ToolbarWidgetWrapper.java:243)
      at android.support.v7.widget.ActionBarOverlayLayout.setWindowTitle(ActionBarOverlayLayout.java:621)
      at android.support.v7.app.AppCompatDelegateImplV9.onTitleChanged(AppCompatDelegateImplV9.java:631)
      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
      at com.spacesoftwares.myapplication3.MainActivity.onCreate(MainActivity.java:20)
      at android.app.Activity.performCreate(Activity.java:6662)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6077)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

解决这个问题的办法大致如下,

第一种情况

版本问题(比如 recycleview的版本跟appcompat的版本不一致),这个问题出现最多,比如我的Android Studio默认使用v26.1.0 (implementation 'com.android.support:appcompat-v7:26.1.0'),而实际最新的Glide4.7.1需要v27。所以我只要在相应的地方修改就好,最终build.gradle(module:app)的结果如下,

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.spacesoftwares.myapplication3"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:27.1.1'
    }
}

repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.bumptech.glide:glide:4.7.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}

可以看到,我的配置是100%按照github【1】上的要求来的,

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.7.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}

第二种情况

在stackover上有一个帖子【2】,其中提到是这玩意在捣蛋,直接删除就OK,

/googlez最新支持kotlin扩展库 预览版
//implementation ‘androidx.core:core-ktx:0.1’

原文是这样描述的:In my case, I was using Android-KTXimplementation 'androidx.core:core-ktx:0.1' in Kotlinproject, and that was the cause of the error. I just removed it from build.gradle as a dependency.

第三种情况

当把Android Studio升级时,可能会碰到,就是在build.gradle中加入以下配置(如果你使用的recycleView的话),

compileSdkVersion 27
buildToolsVersion '27.1.0'

compile 'com.android.support:support-v4:27.1.0'
compile 'com.android.support:appcompat-v7:27.1.0'
compile 'com.android.support:recyclerview-v7:27.1.0'
compile 'com.android.support:design:27.1.0'

不过我没有具体测试过,而且发现如果使用buildToolsVersion '27.1.0'这句,编译阶段就不能通过,会报错:

Error:Failed to find Build Tools revision 27.1.0
<a href="install.build.tools">Install Build Tools 27.1.0 and sync project</a>

所暂时写下来,当是备注吧。

 

【1】https://github.com/bumptech/glide

【2】https://stackoverflow.com/questions/47673261/java-lang-nosuchmethoderror-no-static-method-getfont

posted @ 2018-07-17 11:40  SpaceVision  阅读(433)  评论(0编辑  收藏  举报