android从库项目导入资源
是否可以使用应用程序项目中的库项目中定义的字符串等资源?如果是这样,怎么办? 因为我似乎无法解析我想要解析的字符串:
String title = "sample";
int id = ressources.getIdentifier(title, "string", "com.package");
给我这个异常
WARN/ResourceType(278): No package identifier when getting value for resources number 0x00000000 WARN/System.err(278): android.content.res.Resources$NotFoundException: String Resource ID #0x0
我正在寻找的字符串(“sample”)肯定在库项目的 strings.xml 中提供的这个包中。我什至可以在 R.java 中看到它
is it possible to use ressources like strings that are defined in library projects in the application-projects? if so, how?
because i cant seem to resolve the strings i would like to resolve like this:
String title = "sample";
int id = ressources.getIdentifier(title, "string", "com.package");
gives me this exception
WARN/ResourceType(278): No package identifier when getting value for resource number 0x00000000
WARN/System.err(278): android.content.res.Resources$NotFoundException: String resource ID #0x0
the string ("sample") i am looking for is definitely in this package provided in the strings.xml of the library project. i can even see it in the R.java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以看起来像
返回
0
,这意味着它找不到指定的资源。随后对ressources.getIdentifer()
的调用会导致异常,因为0
不是有效的资源 ID。以下是一些调试/替代想法:
您可能已经这样做了十几次,但是提及一下也无伤大雅:首先重新检查所有内容的拼写:
AndroidManifest.xml
的uses-library
元素中的库拼写是否正确,title
)还是特定于该类型的资源(字符串)?您可以访问其他图书馆的资源吗?Android - 是否可以创建一个自定义库以在多个应用程序中使用?
代码
代码
Android,从字符串获取资源 ID?
http://daniel-codes.blogspot.com/2009/12/dynamically-retriving-resources-in.html
So it looks like
is returning
0
, meaning it can't find the specified resource. The subsequent call toressources.getIdentifer()
causes the exception, since0
is not a valid resource id.Here are some debug/alternative ideas:
You've probably already done this a dozen times, but it doesn't hurt mentioning: first recheck spelling of everything:
uses-library
element theAndroidManifest.xml
,title
) or is it specific to that type of resource(strings)? Can you access the resources of another library?Android - Is it possible to create a custom library to use across several applications?
Code
Code
Android, getting resource ID from string?
http://daniel-codes.blogspot.com/2009/12/dynamically-retrieving-resources-in.html
在您的项目代码中,导入 R 类:
import com.application.libraryproject.R
然后您可以引用任何字符串或其他 xml 定义的资源,如下所示:
String mystring = getString(R. string.app_name);
或类似的。
In your project code, import the R class:
import com.application.libraryproject.R
Then you can reference any string or other xml-defined resource like this:
String mystring = getString(R.string.app_name);
or similar.
我遇到了类似的问题,我使用项目库中的
getPackageName()
方法解决了它。在你的情况下,它应该是:
I had a similar issue and I solved it with the
getPackageName()
method within the Project Library.In your case, it should be:
是的,像平常一样定义库项目中的资源。然后在导入库的项目中引用它们,方法是在库的 AndroidManifest.xml 中在 R 类前面添加包名称。
Yes, define the resources in your library project like normal. Then reference them in the project importing the library by prepending the R class with the package name in the AndroidManifest.xml of the library.