The method getString(int) is undefined for the type myException

android里在一个普通的类中:getString(R.string.the_license_expired)
eclipse 提示:

The method getString(int) is undefined for the type myException

百度了下说先确保获得当前的context,再去在context下获得resources,然后再去从string id获得string就可以了:
First getString is not a static method,
you are calling it in a static context this can’t be done.
Second the getString method is part of the Resources class,
your class does not extend the Resources class so the method can’t be found.

http://stackoverflow.com/questions/12999819/the-method-getstringint-is-undefined-for-the-type-apps

但这是在一个普通的类(未继承Activity)中如何获取到res呢
public class myException extends Exception { ………………

在主界面(只有关闭程序才会finish的activity)中定义public 的context,赋值为自己本身 ,所有类中都可以调用

搜索了一番这种方法应该可行

在继承Activity的MyActivity类中,设置一个静态变量 public static Resources pubr;
在onCreate中初始化 pubr = getResources();
然后在myException类中,通过MyActivity.pubr.getString(R.string.the_license_expired)来获取

测试成功

未经允许不得转载:ganlei的个人博客 » The method getString(int) is undefined for the type myException

赞 (0)
分享到:更多 ()