【问题标题】:Where is the deprecated API at my class code ? I can't figure out我的课程代码中已弃用的 API 在哪里?我想不通
【发布时间】:2019-03-13 23:40:47
【问题描述】:

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

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;
            }
        }
    }

}

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

【问题讨论】:

    标签: java android-studio colors drawable deprecated


    【解决方案1】:

    在 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

    【讨论】:

    • 不知道...现在我正在使用ContextCompat.getColor(context, R.color.color)。不确定如何使用 Resources.Theme 的主题。
    • 如何从 Kotlin 调用 android.content.res.Resources.Theme
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多