Why can't I use Resources.getSystem() without a Runtime error?

冷暖自知 提交于 2019-11-28 01:53:05

According to Android documentation, Resources.getSystem() only provides system-level resources, not application-level ones (like the resources inside your strings.xml file).

http://developer.android.com/reference/android/content/res/Resources.html#getSystem()

Try using the application's context if you really want to retrieve your strings this way, or take my suggestion in the comment to your question.

Using Resources.getSystem().getWhatEver() you can only access system-wide resources (you get the error because there is not system-wide resource with your ID). Since resource ID are not unique across applications you need provide the application when accessing a resource. In Android this is done using Context. So if you want to access some resource you need to use it like this

context.getResources().getString(myID);

Apart from that Brandon's comment is correct.

You could get the context parametr in the function by parameter, or by a static variable, or by getApplicationContext() function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!