Location mapsforge

1

I'm trying to get the location with mapsforge 0.6.1, but in the onCreate() method when assigning latitude and longitude I get this 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)

This is my code

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();

}

}
    
asked by Luis Navarro Arenas 25.07.2016 в 00:56
source

1 answer

1

The variable latLong is null within your onCreate() , you must ensure it has a different value than null, you are defining it:

private LatLong latLong;

but you do not assign value to it, that's why it marks an error in this line of code when calling the getLatitude() method on the instance latLong of value null:

  latitude = latLong.getLatitude();
  longitude = latLong.getLongitude();
    
answered by 25.07.2016 в 17:50