使用情景:在非Activity子类方法中,有时想要调用Activity类特有的方法,系统会提示无该方法The method is undefined。
思路:
将Activity的父类Context作为方法参数,通过context调用该方法。
例一:在无继承类public class LostFocus{ }的方法linearLayoutLostFocus(final LinearLayout linearLayout)中,调用系统服务getSystemService隐藏输入法:
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
eclipse提示出错:
解决方法:将Activity作为方法形参——把方法linearLayoutLostFocus(final LinearLayout linearLayout)
改写成
linearLayout(final LinearLayout linearLayout,final Context context)
,并将getSystemService()
改写成context.getSystemService()
即可解决问题。
完整linearLayoutLostFocus()方法详见:http://blog.csdn.net/youngwm/article/details/48165455
总结:遇到调用的方法(假设为:a())提示The method ** is undefined 时:
1、检查方法a()所在类(假设为:A.class)是否有被当前类引用
2、若没有,尝试将该方法(A.class)作为当前方法的参数,通过A.a()调用该方法。