tags:

views:

122

answers:

1

Hi,

i've got a class public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener

out of this i try to call a method from an other class, this method contains: mFoo.setTextColor(getResources().getColor(R.color.orange))

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

+2  A: 

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.

CommonsWare
Thanks. My origin problem was, that I want to call a Method, belonging to myClass from the Class Preferences. So when a preference has changed, it has to call a method from a "foreign" class, maybe you could give me a hint, how to solve this?!
Christoph
@Christoph: You do not want to be trying to update widgets of some other activity from your `PreferenceActivity`. Have your other activity register for preference changes using `registerOnSharedPreferenceChangeListener()`. When the preference changes, the activity is notified and can make the adjustments.
CommonsWare
Ok, thats what i've done before, i thougt, there is a way to minimize the code in my "other activity", sourcing out the ChangeListener..
Christoph