21

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)
Kamilski81
  • 14,409
  • 33
  • 108
  • 161

2 Answers2

99

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

3lil636wm
  • 7,580
  • 3
  • 24
  • 26
4

if you are using Resource.getColor(int id) method within your fragment then it is deprecated as mentioned in the documentation
So solution is either go for Resource.getColor(int id, Resource.Theme theme) as mention in documentation...but then you need to put it using if condition by checking the Android Version or
You can use ContextCompat class from v4 library as mention here

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26