簡體   English   中英

在主項目中獲取圖書館項目的資源

[英]Getting resources of library project in main project

當我擁有項目main_app時,我可以使用-

int resId = getResources().getIdentifier("xml/" + rule_file, "string", "com.main_app.main");

現在,我的項目main_app是項目test_app的庫項目。

但是,當我運行test_app時,總是會在以下位置得到異常:

int resId = getResources().getIdentifier("xml/" + rule_file, "string", "com.main_app.main"); //Line 5456

錯誤日志-

03-04 15:53:50.158: E/AndroidRuntime(29898): Caused by:
android.content.res.Resources$NotFoundException: Resource ID #0x0
03-04 15:53:50.158: E/AndroidRuntime(29898):  at
android.content.res.Resources.getValue(Resources.java:1017) 03-04
15:53:50.158: E/AndroidRuntime(29898):    at
android.content.res.Resources.loadXmlResourceParser(Resources.java:2102)
03-04 15:53:50.158: E/AndroidRuntime(29898):  at
android.content.res.Resources.getXml(Resources.java:905) 03-04
15:53:50.158: E/AndroidRuntime(29898):    at
com.main_app.main.ClainFragment$23.run(ClainFragment.java:5456)

正如@blackbelt指出的那樣,

軟件包名稱應為test_app項目的軟件包,這是我們在test_app項目的清單文件中定義的軟件包,例如-

 package="com.test_app.main"

所以用這樣的行進行硬編碼-

int resId = getResources().getIdentifier("xml/" + rule_file, "string", "com.main_app.main"); 

錯了。

更改為 -

int resId = getResources().getIdentifier("xml/" + rule_file, "string", getActivity().getPackageName());

有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM