android - 来自 Resources 类型的 getDrawable(int) 在 Eclipse Android 中已弃用

标签 android eclipse

我正在学习一些关于如何创建可以切换静音模式的应用程序的 android 教程。在出现此错误之前一切都很好:

enter image description here

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

你能帮我解决这个问题吗?我是 Android 的新手。顺便说一句,我为此使用 API 23。

最佳答案

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

关于android - 来自 Resources 类型的 getDrawable(int) 在 Eclipse Android 中已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194148/

相关文章:

Android - 如何打开接收分块响应的持久 HTTP 连接?

java - 在 Java 中使用国际化的错误

android - 无法从 Eclipse 3.7.0 启动 android 模拟器

java - 尝试获取应用程序对象时偶尔发生崩溃

安卓工作室 "Cannot start internal HTTP server"

java - 使用 Google API 获取多个位置之间的最佳路径

java - Eclipse 从控制台读取 StdIn(System.in),但不会给出任何输出

eclipse - 如何将eclipse代码风格导入到intellij idea中?

android - 使用 Eclipse DDMS 将大文件推送到模拟器/SD 卡时出现问题

android - 如何使用 adb 命令测量 Micromax C4 中每个核心 CPU 利用率/负载(以 % 为单位)?