以下是被调用的代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
context=this;
Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
MyLocation myLocation=new MyLocation();
try {
MyLocation.getLocation(context, new MyLocationListener() {
@Override
public void gotLocation(Location location) {
SharedPreferences pref=getSharedPreferences(Utilities.USER_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor=pref.edit();
double lat=location.getLatitude();
double lon=location.getLongitude();
editor.putString("lat", lat+"");
editor.putString("lon", lon+"");
Geocoder coder=new Geocoder(MyLocationService.this);
try {
List<Address> addresses=coder.getFromLocation(lat, lon, 1);
String address=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAdminArea();
editor.putString("address", address+"");
editor.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
editor.commit();
}
}
});
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
};
timer.schedule(task, 1000);
return Service.START_STICKY;
}
以下是抛出的异常:
03-10 03:01:25.721: E/AndroidRuntime(912): FATAL EXCEPTION: Timer-0
03-10 03:01:25.721: E/AndroidRuntime(912): at com.barkleyandpaws.services.MyLocationService$1.run(MyLocationService.java:78)
03-10 03:01:25.721: E/AndroidRuntime(912): java.lang.NullPointerException
我只是不确定发生了什么......我真的很感激你可能有的任何见解。该代码的目的是启动服务&amp;获取周期性位置信息的计时器,并将其存储在其他方法用于在地图上显示或按距离排序信息的变量中。
以下是MyLocationService.java的完整代码
package com.barkleyandpaws.services;
import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;
import com.barkleyandpaws.MyLocation;
import com.barkleyandpaws.MyLocation.MyLocationListener;
import com.barkleyandpaws.utils.Utilities;
public class MyLocationService extends Service {
Context context;
@Override
public void onDestroy() {
super.onDestroy();
}
// @Override
// public void onCreate() {
// //Log.i(XXX.LOG, "onCreate");
// super.onCreate();
// }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
context=this;
Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
MyLocation myLocation=new MyLocation();
try {
MyLocation.getLocation(context, new MyLocationListener() {
@Override
public void gotLocation(Location location) {
SharedPreferences pref=getSharedPreferences(Utilities.USER_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor=pref.edit();
double lat=location.getLatitude();
double lon=location.getLongitude();
editor.putString("lat", lat+"");
editor.putString("lon", lon+"");
Geocoder coder=new Geocoder(MyLocationService.this);
try {
List<Address> addresses=coder.getFromLocation(lat, lon, 1);
String address=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAdminArea();
editor.putString("address", address+"");
editor.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
editor.commit();
}
}
});
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
};
timer.schedule(task, 1000);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}