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

前端 未结 2 742
情歌与酒
情歌与酒 2021-02-12 14:10

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         


        
相关标签:
2条回答
  • 2021-02-12 14:50

    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

    0 讨论(0)
  • 2021-02-12 14:52

    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

    0 讨论(0)
提交回复
热议问题