Android : location manager removeUpdates NPE

on Wednesday, October 8, 2014


I have a non activity class MyLocation searching for location which is called form MainActivity. When MainActivity is onPause() I want to remove updates but I get an NPE because the listener = null even it's running. I know I can catch the NPE with an if but my problem is why the listener is null.


I'm calling in the MainActivity @ onPause():



MyLocation myLocation = new MyLocation(MainActivity.this, this);
myLocation.removeListener();


In MyLocation I start the location manager and want to stop listening with the method removeListener():



public boolean getLocation(Context context, LocationResult result) {
// I use LocationResult callback class to pass location value from
// MyLocation to user code.
locationResult = result;
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);

// exceptions will be thrown if provider is not permitted.
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}

// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled)
enableLocationServices(context);

if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);

if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);

timer1 = new Timer();
timer1.schedule(new GetLastLocation(), 60000);
return true;
}

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
// timer1.cancel();
locationResult.gotLocation(location);

ivnetwork = (ImageView) activity.findViewById(R.id.ivnetwork);
ivgps = (ImageView) activity.findViewById(R.id.ivgps);

ivnetwork.setImageResource(R.drawable.ic_network_selected);
ivgps.setImageResource(R.drawable.ic_gps_default);

lm.removeUpdates(this);
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {

locationResult.gotLocation(location);

ivnetwork = (ImageView) activity.findViewById(R.id.ivnetwork);
ivgps = (ImageView) activity.findViewById(R.id.ivgps);

ivgps.setImageResource(R.drawable.ic_gps_selected);
ivnetwork.setImageResource(R.drawable.ic_network_default);

lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
timer1.cancel();

}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

public void removeListener () {
timer1.cancel();
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
}


And here's my gotLocation() from MainActivity which calls a method to show the location:



LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(Location location) {
if (location == null){
tvcity.setText(R.string.notfound);
}
if (location != null) {
setLocation(location);
}
}
};


Here's the log:



10-08 14:16:22.805: W/dalvikvm(4939): threadid=1: thread exiting with uncaught exception (group=0x40c87318)
10-08 14:16:22.805: E/AndroidRuntime(4939): FATAL EXCEPTION: main
10-08 14:16:22.805: E/AndroidRuntime(4939): java.lang.RuntimeException: Unable to pause activity {com.fleank.skybuddy/com.fleank.skybuddy.MainActivity}: java.lang.NullPointerException
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2842)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2798)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2776)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread.access$800(ActivityThread.java:134)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.os.Looper.loop(Looper.java:137)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread.main(ActivityThread.java:4744)
10-08 14:16:22.805: E/AndroidRuntime(4939): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:16:22.805: E/AndroidRuntime(4939): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 14:16:22.805: E/AndroidRuntime(4939): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 14:16:22.805: E/AndroidRuntime(4939): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:16:22.805: E/AndroidRuntime(4939): at dalvik.system.NativeStart.main(Native Method)
10-08 14:16:22.805: E/AndroidRuntime(4939): Caused by: java.lang.NullPointerException
10-08 14:16:22.805: E/AndroidRuntime(4939): at com.fleank.skybuddy.MyLocation.removeListener(MyLocation.java:131)
10-08 14:16:22.805: E/AndroidRuntime(4939): at com.fleank.skybuddy.MainActivity.onPause(MainActivity.java:457)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.Activity.performPause(Activity.java:5106)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1225)
10-08 14:16:22.805: E/AndroidRuntime(4939): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2829)

0 comments:

Post a Comment