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

最后都变了- 提交于 2019-12-04 16:06:57

问题


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)

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/32643596/i-upgraded-android-from-targetsdk-22-to-23-and-im-getting-a-nosuchmethoderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!