机器人 - getResources()和静态 [英] Android - getResources() and static
问题描述
我有一个类
公共类preferences扩展preferenceActivity实现OnShared preferenceChangeListener
出这个我尝试从另一个调用类中的方法。该方法包括:
mFoo.setTextColor(getResources()。的getColor(R.color.orange))
但它不工作。它告诉我 getResources
不是静态的...我怎样才能改变这种?
但它不工作,它告诉我,getResources心不是静态的...我怎样才能改变?
块引用>这意味着你试图调用
getResources()
从一个静态方法,而不是一个常规(实例)方法。最简单的事情在你的情况做的,如果mFoo
是的TextView
或其他一些小部件,是调用getResources()
在上下文
可从窗口小部件:mFoo.setTextColor(mFoo.getContext()getResources()的getColor(R.color.orange));
不过,您试图引用一个名为
mFoo
从静态方法插件的事实,恐慌的废话了我。这只是要求内存泄漏。我觉得你真的需要重新考虑你的静态数据成员和方法的使用。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?解决方案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, ifmFoo
is aTextView
or some other widget, is to callgetResources()
on theContext
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.这篇关于机器人 - getResources()和静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!