1

Estoy intentando obtener la ubicación con mapsforge 0.6.1, pero en el método onCreate() al asignar latitud y longitud me aparece este error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.luis.safe, PID: 27378 java.lang.RuntimeException: Unable to create service com.example.luis.safe.Tracker: java.lang.NullPointerException: Attempt to invoke virtual method 'double org.mapsforge.core.model.LatLong.getLatitude()' on a null object reference at android.app.ActivityThread.handleCreateService(ActivityThread.java:2905) at android.app.ActivityThread.access$1900(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5530) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double org.mapsforge.core.model.LatLong.getLatitude()' on a null object reference at com.example.luis.safe.Tracker.onCreate(Tracker.java:57) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895) at android.app.ActivityThread.access$1900(ActivityThread.java:157)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5530)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 

Este es mi código

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.util.Log;


import com.google.android.gms.common.api.GoogleApiClient;


import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.model.MapViewPosition;
import org.mapsforge.core.model.Point;
import org.mapsforge.core.model.BoundingBox;

public class Tracker extends Service implements LocationListener{
public Tracker() {
}

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

boolean canGetLocation = false;

private static final String TAG = Tracker.class.getSimpleName();

Location location;
double latitude = 0.0;
double longitude = 0.0;

private LatLong latLong;

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 5 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 5 * 1; // 5 segundos, 1000 * 60 * 1 ---> 1 minuto

// Declaring a Location Manager
protected LocationManager locationManager;

@Override
public void onCreate() {
    super.onCreate();

    latitude = latLong.getLatitude();
    longitude = latLong.getLongitude();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //myTask.execute();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location arg0) {
    System.out.println(latitude + "" + longitude);

}

@Override
public void onDestroy() {
    super.onDestroy();

}

}

1 respuesta 1

1

La variable latLong es nula dentro de tu onCreate(), debes asegurar tenga un valor diferente a null, la estas definiendo:

private LatLong latLong;

pero no le asignas valor, por eso marca error en esta linea de código al llamar el método getLatitude() sobre la instancia latLong de valor null:

  latitude = latLong.getLatitude();
  longitude = latLong.getLongitude();

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.