美文网首页Android
Android get resource raw name

Android get resource raw name

作者: JaedenKil | 来源:发表于2018-01-09 16:13 被阅读15次

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.

相关文章

网友评论

    本文标题:Android get resource raw name

    本文链接:https://www.haomeiwen.com/subject/zxcgnxtx.html