Android - getResources() and static

无人久伴 提交于 2020-01-03 09:05:13

问题


I've got a class

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener

out of this I try to call a method from another class. This method contains:

mFoo.setTextColor(getResources().getColor(R.color.orange))

But it doesn't work. It tells me getResources isn't static... how can I change this?


回答1:


But it doesnt work, it tells me, getResources isnt static... how can i change?

This means you are trying to call getResources() from a static method, rather than a regular (instance) method. The easiest thing to do in your case, if mFoo is a TextView or some other widget, is to call getResources() on the Context available from the widget:

mFoo.setTextColor(mFoo.getContext().getResources().getColor(R.color.orange));

However, the fact that you are trying to reference a widget named mFoo from a static method scares the crap out of me. This is just asking for a memory leak. I think you really need to reconsider your use of static data members and methods.



来源:https://stackoverflow.com/questions/3886118/android-getresources-and-static

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