为什么这是不可能的访问资源的静态方法?这是、不可能、静态、方法

2023-09-07 04:15:23 作者:沫你一脸狗屎

现在我知道,如果我要得到一个recource在一些静态的功能,我要通过上下文语境或存在的recources莫名其妙 - 通过参数或通过一个静态变量。但为什么是neccessary?该ID的资源是可达静态的环境中,例如 R.string.some_my_stuff 。如果我想要一个系统资源,还可见有通过 Resources.getSystem()的getString(android.string.some_common_stuff)。但是,为什么我不能做同样的获得应用程序资源的东西吗?资源文件的来源通常的静态部分。资源是静态和属于以应用。应用程序的类属于它以同样的方式和我的可以访问静态方法的静态部分。

I know now, that if I need to get a recource in some static function, I have to pass context or recources of the context there somehow - by parameter or through a static variable. But why is it neccessary? The id's of the resources are reachable in static surroundings, for example R.string.some_my_stuff. If I want a system resource, it is also visible there through Resources.getSystem().getString(android.string.some_common_stuff). But why can't I do something similar to get an application resource? The resource files are the usual static part of the sources. Resources are static and belong to application. The classes of application belong to it in the same way and I can access their static parts in a static way.

为什么我不能用的资源,这将是最自然的,但必须通过实例来访问它们呢?

Why can't I use resources in all the application in same static way, which would be the most natural, but have to access them through instance instead?

我很害怕,我不明白的东西很重要。

I am afraid, I do not understand something very important.

请,不要重复我不能做到这一点。我知道,以我的名誉。请解释原因,或给我指路......只有能治愈我从我的悲伤:-)谢谢。

Please, don't repeat that I can't do it. I know it, on my honour. Please, explain why, or show me the way... Only that will cure me from my sadness :-) Thank you.

推荐答案

资源ID为每个应用程序独一无二的,他们不是唯一在所有应用程序(包括Android系统)。例如。可能有两种不同的字符串,其中具有相同的ID不同的应用,比方说42。 因此,冯·可以访问只有一个应用程序静态(每个程序员都必须同意其中之一就是,它的Andr​​oid系统(有没有选择,始终安装了唯一的一个))。对于所有其他应用程序,你必须能够告诉您要访问的应用程序资源的系统。你利用的情况下。

The resource IDs are unique per application, they are not unique over all application (including the Android system). E.g. there may be two different string in different applications which have the same ID, say 42. Therefore yon may access only one application statically (every programmer must agree which one that is, its the Android system (there no choice, its the only one always installed)). For all the other application you must be able to tell the system which application's resources you want to access. You do this using the context.