该方法getApplicationContext()是未定义 [英] the method getApplicationContext() is undefined

查看:1698
本文介绍了该方法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);

然后创建一个构造在接受上下文参数和使用

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屋!

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