我开发了一个应用程序,其中服务在后台运行。此服务由广播接收器每3分钟启动一次。通过按钮单击启动广播接收器。问题是当我单击按钮启动广播接收器时,我收到以下错误。谁能告诉我出了什么问题?
我的广播接收器如下:
public class k extends BroadcastReceiver{
public static AlarmManager am;
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Intent broadcast = new Intent(context, k.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, broadcast, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+1000, 300000, pendingIntent);
Intent i = new Intent(context,Ser.class);
context.getApplicationContext().startService(i);
} }
我的服务如下:
public class Ser extends Service implements LocationListener {
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String l1=String.valueOf(latitude);
String l2=String.valueOf(longitude);
}
}
return location;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
//Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
DatabaseHandler db=new DatabaseHandler(getApplicationContext());
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
getLocation(LocationManager.NETWORK_PROVIDER);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isNetworkEnabled==true && isGPSEnabled==false)
{
getLocation(LocationManager.NETWORK_PROVIDER);
}
else if(isGPSEnabled==true && isNetworkEnabled==false)
{
getLocation(LocationManager.GPS_PROVIDER);
}
else if(isGPSEnabled==true && isNetworkEnabled==false)
{
getLocation(LocationManager.GPS_PROVIDER);
}
else if(isGPSEnabled==true && isNetworkEnabled==true )
{
getLocation(LocationManager.GPS_PROVIDER);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onLocationChanged(Location location) {
this.location=location;
double latitude =location.getLatitude();
double longitude = location.getLongitude();
DatabaseHandler db=new DatabaseHandler(getApplicationContext());
String l1=String.valueOf(latitude);
String l2=String.valueOf(longitude);
val=db.taskid(l1, l2);
}
这是我的错误日志;
03-21 16:11:37.515: E/AndroidRuntime(22504): FATAL EXCEPTION: main
03-21 16:11:37.515: E/AndroidRuntime(22504): java.lang.RuntimeException: Unable to start service com.example.ishutup.Ser@4105d298 with Intent { cmp=com.example.ishutup/.Ser }: java.lang.NullPointerException
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376)
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.app.ActivityThread.access$1900(ActivityThread.java:123)
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.os.Looper.loop(Looper.java:137)
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-21 16:11:37.515: E/AndroidRuntime(22504): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 16:11:37.515: E/AndroidRuntime(22504): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 16:11:37.515: E/AndroidRuntime(22504): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-21 16:11:37.515: E/AndroidRuntime(22504): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-21 16:11:37.515: E/AndroidRuntime(22504): at dalvik.system.NativeStart.main(Native Method)
03-21 16:11:37.515: E/AndroidRuntime(22504): Caused by: java.lang.NullPointerException
03-21 16:11:37.515: E/AndroidRuntime(22504): at com.example.ishutup.Ser.getLocation(Ser.java:40)
03-21 16:11:37.515: E/AndroidRuntime(22504): at com.example.ishutup.Ser.onStartCommand(Ser.java:61)
03-21 16:11:37.515: E/AndroidRuntime(22504): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
03-21 16:11:37.515: E/AndroidRuntime(22504): ... 10 more
答案 0 :(得分:0)
也许该位置为空。
从LocationManager.getLastKnownLocation文档: 如果当前禁用了提供程序,则返回null