Android-getIdentifier始终返回0(库+应用程序) [英] Android - getIdentifier always returns 0 (library +application)

查看:112
本文介绍了Android-getIdentifier始终返回0(库+应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Android库(com.appocaliptic.quizknife.core)的Android项目(com.appocaliptic.quizknife.app).

I have Android project (com.appocaliptic.quizknife.app) which uses Android library (com.appocaliptic.quizknife.core).

我想做的是获取图片的资源ID(即库).图像的路径为: res/drawable-xhdpi/fr_200_133.png

What I am trying to do, is to get resource id of the picture which is the library. Path to the image is: res/drawable-xhdpi/fr_200_133.png

但是,所有尝试使用getIdentifier的结果均为0.问题出在哪里?

However, all tries with getIdentifier result 0. Where is the problem?

resId = getResources().getIdentifier("fr_200_133", "drawable", "com.appocaliptic.quizknife.core");
resId = getResources().getIdentifier("com.appocaliptic.quizknife.core:drawable/"+"fr_200_133", null, null);
resId = getResources().getIdentifier("drawable/fr_200_133", null, "com.appocaliptic.quizknife.core");

Ach,并且在R.java中有drawable和corensponding属性.

Ach, and in R.java there is drawable and corensponding attribute.

推荐答案

您不应使用库包名称.尝试以下方法:

You should not be using the library package name. Try this instead:

resId = getResources().getIdentifier("fr_200_133", "drawable", getPackageName());

(如果在视图中执行,则为 getContext().getPackageName()).

(or getContext().getPackageName() if this is executing in a view).

关键是您需要使用应用程序的软件包名称(如清单中所示),而不是库的软件包名称(在创建应用程序时实际上会消失).

The key is that you need to use the app's package name (as listed in the manifest) rather than the library's package name (which actually disappears when creating the app).

这篇关于Android-getIdentifier始终返回0(库+应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆