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.
网友评论