Android - getResources () and static

I have a class

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener 

From this I am trying to call a method from another class. This method contains:

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

But that will not work. This tells me that getResources not static ... how can I change this?

+6
android static
source share
1 answer

But it does not work, it tells me that getResources is not static ... how can I change?

This means that you are trying to call getResources() from a static method, and not from a regular (instance) method. The simplest thing in your case, if mFoo is a TextView or some other widget, call getResources() in the Context accessible 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 me. It just requires a memory leak. I think you really need to rethink the use of static data elements and methods.

+13
source share

All Articles