事实上,我无法确定哪个上下文作为参数传递给GPSTracker(上下文),这是一个错误。
我的接收者代码的一部分:
public class AlarmActivity extends BroadcastReceiver {
GPSTracker gps;
@Override
public void onReceive(Context context, Intent intent) {
gps = new GPSTracker(context);//The context am using here is giving the error
Log.i("happening","in alarm reciever class");
}
}
GPSTracker类的一部分:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
location.setAccuracy(3);
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
我收到以下错误:
03-03 12:23:47.819: W/System.err(1053): java.lang.NullPointerException
03-03 12:23:47.819: W/System.err(1053): at com.example.mehweez.GPSTracker.getLocation(GPSTracker.java:64)
03-03 12:23:47.819: W/System.err(1053): at com.example.mehweez.GPSTracker.<init>(GPSTracker.java:44)
03-03 12:23:47.819: W/System.err(1053): at com.example.mehweez.AlarmActivity.onReceive(AlarmActivity.java:44)
03-03 12:23:47.829: W/System.err(1053): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
03-03 12:23:47.829: W/System.err(1053): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
03-03 12:23:47.829: W/System.err(1053): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
03-03 12:23:47.829: W/System.err(1053): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 12:23:47.829: W/System.err(1053): at android.os.Looper.loop(Looper.java:137)
03-03 12:23:47.829: W/System.err(1053): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-03 12:23:47.829: W/System.err(1053): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 12:23:47.839: W/System.err(1053): at java.lang.reflect.Method.invoke(Method.java:525)
03-03 12:23:47.839: W/System.err(1053): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-03 12:23:47.839: W/System.err(1053): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 12:23:47.839: W/System.err(1053): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您似乎在setAccuracy()
对象上调用了Location
。这有两个问题:
您尚未初始化location
数据成员。
调用setAccuracy()
没用。
答案 1 :(得分:0)
GPSTracker是一项服务,而AlarmActivity是一个broadcastReceiver,如果你需要从broadcastReceiver调用服务,你需要这个:
public class AlarmActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent=new Intent(context,GPSTracker.class);
context.startService(myIntent);
Log.i("happening","in alarm reciever class");
}
}