![](/img/trans.png)
[英]Resources.getSystem().openRawResource(random.txt) returns Resource$NotFoundException
[英]Android: Resources.getSystem().getIdentifier returns java.lang.IllegalArgumentException
我正在嘗試通過名稱someId = "hd_2"
獲得圖片的ID 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。
從Resources.getSystem()
的文檔中 :
返回一個全局共享資源對象,該對象僅提供對系統資源(無應用程序資源)的訪問,並且沒有為當前屏幕進行配置(無法使用尺寸單位,不會根據方向發生變化,等等)。
您正在尋找錯誤的Resources
對象。 您需要從當前Context
獲取實例。 就像是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
int resId = res.getIdentifier(someId, "drawable", getPackageName());
}
也許這可以幫助您: https : //stackoverflow.com/a/15488321/3633665
int resId=YourActivity.this.getResources().getIdentifier("res_name", "drawable", YourActivity.this.getPackageName());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.