Android get resource raw name

In android instrument test, when we want to add a resource file raw name to log,
it's supposed to be:

Resources.getResourceEntryName(resourceId)

But which shows an error:

non-static method cannot be referenced from a static context

And it is indeed:

public String getResourceEntryName(int resid) throws Resources.NotFoundException {
        throw new RuntimeException("Stub!");
    }

In fact we can do:

Context mContext = InstrumentationRegistry.getTargetContext();
mContext.getResources().getResourceEntryName(resourceId)

You can't call something that doesn't exist. Since you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists.

The real problem is: getResourceEntryName() method itself is not static, it's an instance-level method, so we have to make an instance of Resources first.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,980评论 0 23
  • ① 上周六、周日,整整两天的时间,我足不出户,埋头在家清理物品。 这是一件下过无数次决心要做的、但我一直在拖延着不...
    阿褒阅读 1,224评论 16 23
  • 此时离培训结束已经有五天了,脑海中那些感性的东西已逐渐褪去,然而往往沉淀下来的东西才最深刻,或许我记不住当时的...
    松鼠症阅读 255评论 0 0
  • 让往事随风而去,岁月静好,亦无忧愁…… 喜欢李冰冰最近说的一句话:‘’现在才是最好的年龄,每一个阶段都不会觉得自己...
    文若男阅读 327评论 4 8
  • 沐夏之梦阅读 243评论 1 0