java - Android:Resources.getSystem().getIdentifier 返回 java.lang.IllegalArgumentException

标签 java android resources

我正在尝试按名称获取图片的 ID someId =“hd_2” Resources.getSystem().getIdentifier(someId, "drawable", getPackageName());

public static final class drawable {
    ...
    public static final int hd_2=0x7f020088;
    ...
}

为什么它可能返回 java.lang.IllegalArgumentException :无效方法?

更新:

我将 Resources.getSystem().getIdentifier 从 onCreate 移至 onStart,现在它返回 0。

最佳答案

来自documentationResources.getSystem()上:

Return a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).

您正在查找错误的Resources对象。您需要从当前的Context 获取实例。像这样的东西:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources res = getResources();
    int resId = res.getIdentifier(someId, "drawable", getPackageName());
}

关于java - Android:Resources.getSystem().getIdentifier 返回 java.lang.IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28619487/

相关文章:

java - 在异步任务中不断出现空指针异常

java - Android 什么时候可以释放端口?

android - 尝试导入新项目示例时遇到问题

android - RecyclerView 在回收 View 时不尊重翻译

Qt:如何关闭配置文件中的资源压缩?

java - IDEA - 无法加载java资源

c# - .NET 全局化和卫星 DLL

java - 在 Mock 对象上调用方法是调用真实方法而不是模拟实现

java - 如何在 Java 8 中使用 CompletableFuture 启动异步任务并让主线程完成并退出

java - 欧拉计划问题3 索引越界异常