Frage

Ich habe einen GPS-Fehler in meiner App, wenn ich versuche, meinen aktuellen Standort per GPS oder Netzwerk abzurufen.

private static LocationManager lm;
private static LocationListener locationListener;
private static Location posicion;   
private double latitud;
private double longitud;

posicion = ObtenPosicion();       
        latitud = posicion.getLatitude();
        longitud= posicion.getLongitude();

        //Me centro en el mapa
        final GeoPoint yo = new GeoPoint((int)(latitud),(int)(longitud));   

 public Location ObtenPosicion(){
     if (lm == null)
        {
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

            locationListener = new LocListener();

            if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
            {
                lm.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 
                    LOCATION_UPDATE_TIME, 
                    LOCATION_UPDATE_DISTANCE, 
                    locationListener);

                posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }

            if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            {
                        lm.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 
                        LOCATION_UPDATE_TIME, 
                        LOCATION_UPDATE_DISTANCE,  
                        locationListener);

                posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
        }
        return posicion;
    }



 private class LocListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
           posicion.set(loc);
        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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


    }  

    public static Location getCurrentLocation ()
    {
        return posicion;
    }

    public void stopLocationListening ()
    {
        if (lm != null)
            lm.removeUpdates(locationListener);

    }

Der Fehler ist eine Nullzeigerausnahme in einer Zeile:

12-06 17:04:25.418: E/AndroidRuntime(4744): java.lang.NullPointerException
12-06 17:04:25.418: E/AndroidRuntime(4744):     at com.rbrlnx.lugares.editarLugar$LocListener.onLocationChanged(editarLugar.java:215)

Ich habe es mit einem anderen Code versucht, aber es ist mir unmöglich.

War es hilfreich?

Lösung

Ihr Problem ist die Tatsache, dass Sie kein 'posicio'-Objekt zugewiesen haben.Sie haben es einfach als statisch deklariert.Wenn Sie also den Rückruf vom System im onLocationChanged-Rückruf erhalten, versuchen Sie, ein Nullobjekt zu verwenden.

In Ihrer onCreate-Methode müssen Sie die Position zuweisen und initialisieren mit:

posicion = new Location();

Andere Tipps

Die Positionsvariable ist hier null .... Initialisieren Sie sie und versuchen Sie eine / beide der beiden

Initialisieren Sie bei der onCreate()-Methode die Position mit einem Standardwert.Und / oder rufen Sie ObtenPosicion() an der entsprechenden Stelle mit try / catch auf, falls erforderlich

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top