我的类代码中不推荐使用的API在哪里?我弄不清楚

问题描述 投票:0回答:1

这是一个简单的代码,错误消息并没有说明不推荐使用的确切位置。

enter image description here

package com.dev.marcellocamara.pgm.Helper;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.dev.marcellocamara.pgm.R;

public class CardHelper {

    public static Drawable getBackground(Context context, int drawable){
        switch (drawable){
            case 1 : {
                return (context.getResources().getDrawable(R.drawable.card_yellow));
            }
            case 2 : {
                return (context.getResources().getDrawable(R.drawable.card_purple));
            }
            case 3 : {
                return (context.getResources().getDrawable(R.drawable.card_green));
            }
            default : {
                return null;
            }
        }
    }

    public static Drawable getFlag(Context context, int flag) {
        switch (flag){
            case 1 : {
                return (context.getResources().getDrawable(R.drawable.flag1));
            }
            case 2 : {
                return (context.getResources().getDrawable(R.drawable.flag2));
            }
            case 3 : {
                return (context.getResources().getDrawable(R.drawable.flag3));
            }
            default : {
                return null;
            }
        }
    }

    public static int getColor(Context context, int card) {
        switch (card){
            case 1 : {
                return (context.getResources().getColor(R.color.card1));
            }
            case 2 : {
                return (context.getResources().getColor(R.color.card2));
            }
            case 3 : {
                return (context.getResources().getColor(R.color.card3));
            }
            default : {
                return 0;
            }
        }
    }

}

正如代码所说,我正在返回drawable或颜色。这很简单。需要更改的警告在哪里?如何跳过此警告,继续返回drawable和颜色?

java android-studio colors drawable deprecated
1个回答
4
投票

在Android API 22中,getResources().getDrawable()已被弃用。现在你应该使用这个方法getDrawable (int id, Resources.Theme theme)

getColor (int id)在API 23中被弃用。现在使用getColor(int, android.content.res.Resources.Theme)代替。

查看更多信息:https://developer.android.com/reference/android/content/res/Resources

© www.soinside.com 2019 - 2024. All rights reserved.