Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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 虽然在代码和清单中正确声明,但缺少android服务_Java_Android_Android Intent_Nullpointerexception_Android Manifest - Fatal编程技术网

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

Java 虽然在代码和清单中正确声明,但缺少android服务,java,android,android-intent,nullpointerexception,android-manifest,Java,Android,Android Intent,Nullpointerexception,Android Manifest,我收到一个错误,好像找不到我正在使用的LocationService 我错过了什么 这是我的舱单: <activity android:name="com.example.manyexampleapp.StoresListActivity" > <intent-filter> <action android:name="android.inte

我收到一个错误,好像找不到我正在使用的LocationService 我错过了什么

这是我的舱单:

        <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" />
我的位置服务课

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);
}

尝试以这种方式启动您的服务:

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

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

希望有帮助。

对于启动服务,请使用此选项

 startService(new Intent(this, LocationService.class));
而不是

 LocationService locService = new LocationService();

 locService.startService(new Intent());

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

locService.startService(newintent())

按如下方式触发您的服务:

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

context.startService(i); 

你应该开始这样的服务

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>

...
是细节

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