Eclipse Android中不推荐使用Resources类型的getDrawable(int)

时间:2016-02-04 06:39:38

标签: android eclipse

我正在关注如何创建可以切换静音模式的应用程序的android教程。一切都很好,直到我得到这个错误:

enter image description here

The method getDrawable(int) from the type Resources is deprecated

你能帮我解决这个问题吗?我是Android新手。顺便说一下,我正在使用API​​ 23。

4 个答案:

答案 0 :(得分:5)

getResources().getDrawable()现已弃用。

您应该使用支持库中的以下代码:

ContextCompat.getDrawable(context, R.drawable.***)

使用此方法相当于调用:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}

从API 21开始,您应该使用getDrawable(int, Theme)方法而不是getDrawable(int),因为它允许您获取与给定屏幕密度/主题的特定资源ID关联的可绘制对象。调用已弃用的getDrawable(int)方法等同于调用getDrawable(int, null)

了解更多详情,请访问此处。

Android getResources().getDrawable() deprecated API 22

答案 1 :(得分:1)

这只是一个警告,告诉您谷歌不支持此方法,您不应该使用它。你可以使用getDrawable(R.drawable.phone_silent,null);此null用于主题。如果你有特定的主题,那么你可以在这里提供主题null就足够了。 如果有帮助请接受。

答案 2 :(得分:1)

这张图片可能会解决您的问题

this is Documentation given by Android itself

答案 3 :(得分:1)

getDrawable(int) method was deprecated in API level 22.

使用 getDrawable(int,Theme)