虽然在代码和清单中正确声明但缺少android服务

时间:2014-01-24 11:26:05

标签: java android android-intent nullpointerexception android-manifest

我收到错误,好像无法找到我正在使用的LocationService 我错过了什么?

enter image description here

这是我的清单:

        <activity 
            android:name="com.example.manyexampleapp.StoresListActivity" >
                        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<service android:enabled="true" android:name="com.example.manyexampleapp.LocationService" />

我的StoresListActivity类:

package com.example.manyexampleapp;

public class StoresListActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        LocationService locService = new LocationService();

        locService.startService(new Intent());
    }

我的LocationService类

package com.example.manyexampleapp;

public class LocationService extends Service {
public static final String LOCATION_BROAD_MSG = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;

public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;

Intent intent;
int counter = 0;

@Override
public void onCreate() {
    super.onCreate();
    intent = new Intent(LOCATION_BROAD_MSG);      
}

@Override
public void onStart(Intent intent, int startId) {      
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listener = new MyLocationListener();        
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
}

4 个答案:

答案 0 :(得分:0)

尝试以这种方式开始服务:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    startService(new Intent(this, LocationService.class));
}

希望它有所帮助。

答案 1 :(得分:0)

开始使用此服务

 startService(new Intent(this, LocationService.class));

而不是

 LocationService locService = new LocationService();

 locService.startService(new Intent());

答案 2 :(得分:0)

请在StoresListActivity中检查您的意图对象。 ,即

locService.startService(new Intent());

触发您的服务:

Intent i= new Intent(context, CustomService.class);

context.startService(i); 

答案 3 :(得分:0)

你应该开始这样的服务。

public void onCreate(Bundle savedInstanceState) {    
      super.onCreate(savedInstanceState);  
      Intent intent=new Intent("com.example.manyexampleapp.LocationService");  
      this.startService(intent);
}

并在您的Android清单文件中提及所需的许可,即

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
</manifest>

Here是详细信息。