机器人 - getResources()和静态 [英] Android - getResources() and static

查看:228
本文介绍了机器人 - getResources()和静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类

 公共类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, 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.

这篇关于机器人 - getResources()和静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆