无法使用 Mapsforge 获取位置

Can't get location with Mapsforge

我正在尝试使用 Mapsforge 0.6.1 获取纬度和经度,但出现错误。我正在使用 Android Studio。

Logcat:

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.access00(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.access00(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) 

这是我的代码。在onCreate方法中,我调用了getLatitude,出现了错误。

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

    }
}

正如日志所说:latLongnull。在初始化 latLong 之前,您不能 getLatitude()getLongitude()。在 onLocationChanged(Location arg0) 中初始化 latLong。并查看此 code 以了解您还应该做什么。