android从库项目导入资源

发布于 2024-10-13 05:15:36 字数 473 浏览 4 评论 0原文

是否可以使用应用程序项目中的库项目中定义的字符串等资源?如果是这样,怎么办? 因为我似乎无法解析我想要解析的字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぺ禁宫浮华殁 2024-10-20 05:15:36

所以看起来像

int id = ressources.getIdentifier(title, "string", "com.package");

返回0,这意味着它找不到指定的资源。随后对 ressources.getIdentifer() 的调用会导致异常,因为 0 不是有效的资源 ID。

以下是一些调试/替代想法:


  • 您可能已经这样做了十几次,但是提及一下也无伤大雅:首先重新检查所有内容的拼写:

    • 包拼写正确(在库项目和客户端项目中),
    • 资源字符串正确(库项目和客户端项目),
    • AndroidManifest.xmluses-library 元素中的库拼写是否正确,
    • 等等

  • 中的库拼写是否正确。您能否访问该库中的任何资源,或者问题是否特定到该资源(title)还是特定于该类型的资源(字符串)?您可以访问其他图书馆的资源吗?

  • 您是否以 jar 文件的形式访问该库?您可以将代码打包,但无法从 jar 访问资源。

Android - 是否可以创建一个自定义库以在多个应用程序中使用?


  • 您是否尝试过替代名称格式:

代码

String fullyQualifiedResourceName = "com.package:string/sample";
int id = ressources.getIdentifier(title, null, null);
if (id == 0) {
    Log.e(TAG, "Lookup id for resource '"+fullyQualifiedResourceName+"' failed";
    // graceful error handling code here
}

  • 您可以尝试使用反射:

代码

final String resourceName= "sample";
final int id;
try {
    final Class resourceType = com.package.R.string.class;
    final Field field = resourceType.getField(title);
    id = field.getInt(null);
} catch (final Exception e) {
    Log.e(TAG, "Lookup id for resource '"+resourceName+"' failed";
    // graceful error handling code here
}

Android,从字符串获取资源 ID?

http://daniel-codes.blogspot.com/2009/12/dynamically-retriving-resources-in.html

So it looks like

int id = ressources.getIdentifier(title, "string", "com.package");

is returning 0, meaning it can't find the specified resource. The subsequent call to ressources.getIdentifer() causes the exception, since 0 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:

    • package spelling is correct (both in library project and in client project),
    • resource string is correct (library project and client project),
    • is library spelling correct in the uses-library element the AndroidManifest.xml,
    • etc.

  • Can you access any resources in that library or is the problem specific to that resource (title) or is it specific to that type of resource(strings)? Can you access the resources of another library?

  • Are you accessing the library as a jar file? You can jar the code, but you can't access resources from a jar.

Android - Is it possible to create a custom library to use across several applications?


  • Did you try the alternate name format:

Code

String fullyQualifiedResourceName = "com.package:string/sample";
int id = ressources.getIdentifier(title, null, null);
if (id == 0) {
    Log.e(TAG, "Lookup id for resource '"+fullyQualifiedResourceName+"' failed";
    // graceful error handling code here
}

  • You could try using reflection:

Code

final String resourceName= "sample";
final int id;
try {
    final Class resourceType = com.package.R.string.class;
    final Field field = resourceType.getField(title);
    id = field.getInt(null);
} catch (final Exception e) {
    Log.e(TAG, "Lookup id for resource '"+resourceName+"' failed";
    // graceful error handling code here
}

Android, getting resource ID from string?

http://daniel-codes.blogspot.com/2009/12/dynamically-retrieving-resources-in.html

花开雨落又逢春i 2024-10-20 05:15:36

在您的项目代码中,导入 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.

彻夜缠绵 2024-10-20 05:15:36

我遇到了类似的问题,我使用项目库中的 getPackageName() 方法解决了它。

在你的情况下,它应该是:

String title = "sample";
int id = ressources.getIdentifier(title, "string", getPackageName());

I had a similar issue and I solved it with the getPackageName() method within the Project Library.

In your case, it should be:

String title = "sample";
int id = ressources.getIdentifier(title, "string", getPackageName());
不爱素颜 2024-10-20 05:15:36

是的,像平常一样定义库项目中的资源。然后在导入库的项目中引用它们,方法是在库的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文