Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getApplicationContext()引发nullpointexception_Java_Android_Android Service - Fatal编程技术网

Java getApplicationContext()引发nullpointexception

Java getApplicationContext()引发nullpointexception,java,android,android-service,Java,Android,Android Service,我正在尝试访问服务中的SharedReference public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { Context context = getApplicationContext();

我正在尝试访问服务中的SharedReference

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)
        ...
那一行也开始变为空。我的研究使我使用
getApplicationContext()
作为上下文。但我每转一圈都是空的

LocationService.
听起来像构造器。其他帖子似乎说我不能在那里调用首选项。但是,如果我尝试在onCreate中创建SharedRef,它仅在该方法中可用。是否有某种方法可以在整个服务中提供首选项

尝试更换

Context ctx = getApplicationContext();

LocationService.
听起来像构造器

实际上,它是数据成员的初始值设定项

但是,如果我尝试在onCreate中创建SharedRef,它仅在该方法中可用

欢迎您在
onCreate()
之外声明数据成员。只有在
onCreate()
内,才能初始化数据成员:


服务
上下文
<代码>服务扩展
ContextWrapper
ContextWrapper
扩展
Context
,因此您所要做的就是:

Context context = this;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

希望对你有帮助

好的,这是因为这个类(这个问题中的服务)没有实例化,所以“this”是空的。还有,我发现了一个和你一样有趣的问题。也许会有帮助

我认为你应该这样做: sharedPref=PreferenceManager.getDefaultSharedPreferences(此);
按照CommonWare的建议,在onCreate()之后。

重写onCreate并将其移动到itI中。我想知道为什么这个问题被否决。根据:
如果你的动机是“我希望其他人向我解释”,那么你可能没问题。
然后
GetDefaultSharedReferences
抛出NullPointerException然后
GetDefaultSharedReferences(上下文)
在创建并使用它时抛出nullpointerexceptionoverride,或者您必须在onStart命令中使用它
Context ctx = this;
public class LocationService extends Service {
  private SharedPreferences sharedPref;

  public void onCreate() {
    super.onCreate();

    sharedPref=PreferenceManager.getDefaultSharedPreferences(this);
  }

  // other cool stuff here
}
Context context = this;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);