I upgraded Android from targetSdk 22 to 23 and i'm getting a NoSuchMethodError. How could i fix this?

Kamilski81 picture Kamilski81 · Sep 18, 2015 · Viewed 13.6k times · Source

Here are is my config and my excpetion, i'm not sure how to fix this?

compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.rithmio.coach"
    minSdkVersion 19
    targetSdkVersion 23
}

09-17 22:52:15.645  15249-15249/com.rithmio.coach E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.rithmio.coach, PID: 15249
    java.lang.NoSuchMethodError: No virtual method getColor(ILandroid/content/res/Resources$Theme;)I in class Landroid/content/res/Resources; or its super classes (declaration of 'android.content.res.Resources' appears in /system/framework/framework.jar)
            at com.rithmio.coach.mobile.fragment.WorkoutsListFragment.onCreateView(WorkoutsListFragment.java:55)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
            at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:511)
            at com.rithmio.coach.mobile.MobileMainActivity.onStart(MobileMainActivity.java:269)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
            at android.app.Activity.performStart(Activity.java:6006)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Answer

Melvin picture Melvin · Oct 18, 2015

You need to use ContextCompat.getColor(), which is part of the Support V4 Library (so it will work for all the previous API).

ContextCompat.getColor(context, R.color.my_color)

You will need to add the Support V4 library by adding the following to the dependencies array inside your app build.gradle:

compile 'com.android.support:support-v4:23.0.1'

If you care about theming, the documentation specifies that the method will use the context's theme:

Starting in M, the returned color will be styled for the specified Context's theme