2017-05-04 4 views
0

So versuche ich eine Kartenaktivität, die die Länge und Breite meiner aktuellen Position in einem Toast anzeigt, aber ich bekomme eine Null object reference, obwohl das Objekt ist instanziiert eine Zeile, bevor ich es referenziere. currentLocation ist oben bei create deklariert.Android Karten Aktivität Null Objektreferenz, aber ich verstehe nicht warum

Fehlermeldung verweist auf zweite/dritte letzte Zeile Code:

java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.location.Location.mProvider' on a null object reference 

Relevante Code:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    //Initialize Google Play Services 
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 
     } 
    } else { 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
    } 

    currentLocation = new Location(LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient)); 
    Toast.makeText(this, currentLocation.toString(), Toast.LENGTH_LONG).show(); 
} 
+0

Laut der Dokumentation: "Wenn ein Ort ist nicht verfügbar, was sehr selten passieren sollte, null wird zurückgegeben " – Eselfar

Antwort

0

getLastLocation innerhalb onMapReady Ihre letzte Position überprüfen, die null sein kann, wenn Sie es verwenden Das erste Mal, so müssen Sie dies in onConnected schreiben

@Override 
public void onConnected(@Nullable Bundle bundle) { 

    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(3000); 
    mLocationRequest.setFastestInterval(3000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    if (ContextCompat.checkSelfPermission(getActivity(), 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 



    mGoogleApiClient.isConnected(); 

    System.out.println("mGoogleApiClient.isConnected() " + mGoogleApiClient.isConnected()); 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 


    System.out.println("mLastLocation " + mLastLocation); 
    if (mLastLocation != null) { 


    } 

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 



} 
+0

Ich habe diesen Code in meiner onCreate-Methode implementiert, aber der Fehler hat sich nicht geändert – killiantos

+0

Nein, Sie müssen GoogleApiClient.ConnectionCallbacks für Ihre Aktivität implementieren, dann wird onConnected überschrieben Sie müssen diesen Code verwenden. –

+0

@killiantos hast du die Lösung bekommen? –

Verwandte Themen