我正在尝试按名称获取图片的 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。
最佳答案
来自documentation在Resources.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/