该方法getApplicationContext()是未定义 [英] the method getApplicationContext() is undefined
本文介绍了该方法getApplicationContext()是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT);
}
因此,这是我应得的,它显示了一个错误,并告诉我的方法getApplicationContext()是未定义的类型MyLocationListener
So this is what I have, and it shows an error and tells me The method getApplicationContext() is undefined for the type MyLocationListener
我应该怎么做才能避免这个错误
what should I do to avoid this error
推荐答案
既然你是不是在活动
你需要传递一个上下文
的类。无论你实例化该类通过你的活动方面
Since you aren't in an Activity
you need to pass a Context
to the class. Wherever you instantiate this class pass your Activities context
MyClass myClass = new MyClass(this);
然后创建一个构造
在接受上下文
为参数该类别code>和使用
public class MyClass
{
Context c;
public MyClass(Context context)
{
c= context;
}
}
那么当你需要使用它
then when you need to use it
public void onProviderDisabled(String provider) {
Toast.makeText(c, "Gps Disabled", Toast.LENGTH_SHORT);
}
这篇关于该方法getApplicationContext()是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文