在服务中使用systemservice

时间:2013-11-20 22:53:03

标签: android android-intent service android-context

我正在开始服务:

 Intent intent = new Intent(this, Service_Location_Manager.class);
                    startService(intent);

然后该服务给出一个空指针:

 public Service_Location_Manager() {
    super("Service_Location_Manager");

    wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

  }

如何使用SystemService?我试过这个,this.getApplicationContext,this.getBaseContext,但它都给出了一个空指针:

11-20 14:48:05.305: E/AndroidRuntime(6621): Caused by: java.lang.NullPointerException
11-20 14:48:05.305: E/AndroidRuntime(6621):     at android.content.ContextWrapper.getSystemService(ContextWrapper.java:393)
11-20 14:48:05.305: E/AndroidRuntime(6621):     at com.testapp.Service_Location_Manager.<init>(Service_Location_Manager.java:57)
11-20 14:48:05.305: E/AndroidRuntime(6621):     at java.lang.Class.newInstanceImpl(Native Method)
11-20 14:48:05.305: E/AndroidRuntime(6621):     at java.lang.Class.newInstance(Class.java:1319)
11-20 14:48:05.305: E/AndroidRuntime(6621):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2246)

0 个答案:

没有答案