getApplicationContext() 抛出空指针异常
getApplicationContext() throws nullpointexception
我正在尝试访问服务中的 SharedPreferences。
public class LocationService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
Context context = getApplicationContext();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
...
但是我得到这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service us.keithirwin.myapp.LocationService: java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
at us.keithirwin.myapp.LocationService.<init>(LocationService.java:31)
...
LocationService.java:31 指的是 Context context = getApplicationContext();
。
我曾经使用 this
作为上下文,即
PreferenceManager.getDefaultSharedPreferences(this);
该行也开始出现空值。我的研究使我使用 getApplicationContext()
作为我的上下文。但我转向的每一个方向都是空的。
LocationService.<init>
听起来像构造函数。其他帖子似乎说我不能在那里调用首选项。但是,如果我尝试在 onCreate 中创建 sharedPref,它仅在该方法中可用。有什么方法可以让偏好在整个服务中可用?
尝试替换
Context ctx = getApplicationContext();
和
Context ctx = this;
LocationService.<init>
sounds like the constructor.
实际上,它是一个数据成员的初始化程序。
However, if I try to create sharedPref in onCreate, it's only available in that method.
欢迎您在 onCreate()
之外声明数据成员。您无法 初始化 数据成员,直到进入 onCreate()
:
public class LocationService extends Service {
private SharedPreferences sharedPref;
public void onCreate() {
super.onCreate();
sharedPref=PreferenceManager.getDefaultSharedPreferences(this);
}
// other cool stuff here
}
Service
是 Context
。 Service
extends ContextWrapper
and ContextWrapper
extends Context
因此你所要做的就是:
Context context = this;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
希望对您有所帮助!!!
ok,因为这个class(本题中的Service)还没有实例化,所以"this"为null。另外,我发现了一个和你一样有趣的问题 Can "this" ever be null in Java?。可能有帮助
我认为你应该这样做:
sharedPref=PreferenceManager.getDefaultSharedPreferences(这个);
按照 CommonWare 的建议在 onCreate() 之后。
我正在尝试访问服务中的 SharedPreferences。
public class LocationService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
Context context = getApplicationContext();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
...
但是我得到这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service us.keithirwin.myapp.LocationService: java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
at us.keithirwin.myapp.LocationService.<init>(LocationService.java:31)
...
LocationService.java:31 指的是 Context context = getApplicationContext();
。
我曾经使用 this
作为上下文,即
PreferenceManager.getDefaultSharedPreferences(this);
该行也开始出现空值。我的研究使我使用 getApplicationContext()
作为我的上下文。但我转向的每一个方向都是空的。
LocationService.<init>
听起来像构造函数。其他帖子似乎说我不能在那里调用首选项。但是,如果我尝试在 onCreate 中创建 sharedPref,它仅在该方法中可用。有什么方法可以让偏好在整个服务中可用?
尝试替换
Context ctx = getApplicationContext();
和
Context ctx = this;
LocationService.<init>
sounds like the constructor.
实际上,它是一个数据成员的初始化程序。
However, if I try to create sharedPref in onCreate, it's only available in that method.
欢迎您在 onCreate()
之外声明数据成员。您无法 初始化 数据成员,直到进入 onCreate()
:
public class LocationService extends Service {
private SharedPreferences sharedPref;
public void onCreate() {
super.onCreate();
sharedPref=PreferenceManager.getDefaultSharedPreferences(this);
}
// other cool stuff here
}
Service
是 Context
。 Service
extends ContextWrapper
and ContextWrapper
extends Context
因此你所要做的就是:
Context context = this;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
希望对您有所帮助!!!
ok,因为这个class(本题中的Service)还没有实例化,所以"this"为null。另外,我发现了一个和你一样有趣的问题 Can "this" ever be null in Java?。可能有帮助
我认为你应该这样做: sharedPref=PreferenceManager.getDefaultSharedPreferences(这个); 按照 CommonWare 的建议在 onCreate() 之后。