Search code examples
androidserviceforcecloselocationlistener

Why is the Service making the app to force close?


I am newbie to Android and need help to fix this problem. I have a Service which implements a LocationListener. I am getting the following errors when the app crashes. Please explain what changes I should make in my codes. I have referred to many LocationListener and Services tutorial but can't exactly figure out what is going on. Please clear my doubts step by step. I am posting my code and error logs:

New Error log:

 03-22 01:42:20.249: E/AndroidRuntime(6537): FATAL EXCEPTION: main
 03-22 01:42:20.249: E/AndroidRuntime(6537): java.lang.NullPointerException
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at com.example.broadcast.Ser.onLocationChanged(Ser.java:131)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:263)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:196)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:212)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at android.os.Handler.dispatchMessage(Handler.java:99)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at android.os.Looper.loop(Looper.java:137)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at android.app.ActivityThread.main(ActivityThread.java:4960)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at java.lang.reflect.Method.invokeNative(Native Method)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at java.lang.reflect.Method.invoke(Method.java:511)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 03-22 01:42:20.249: E/AndroidRuntime(6537):    at dalvik.system.NativeStart.main(Native Method)

And here is my (new)service code:

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;
}
 }
  public void onLocationChanged(Location location) {

    if(location!=null)
    {
    double latitude =location.getLatitude();
    double longitude = location.getLongitude();
    Log.i("msg","in locationchanged");

    }
   }

Solution

  • In

    public void onLocationChanged(Location location) {
    
        double latitude =location.getLatitude();
        double longitude = location.getLongitude();
        Log.i("msg","in locationchanged");
    
    }
    

    Check if location is null.

    public void onLocationChanged(Location location) {
        if (location == null) return;
    
        double latitude =location.getLatitude();
        double longitude = location.getLongitude();
        Log.i("msg","in locationchanged");
    
    }