2017-07-05 2 views
0

Ich erhalte eine Nullzeigerausnahme beim Aufrufen von location.getLatitude in der initCamera-Methode. Ich habe alle Lösungen auf Stack versucht, aber keiner hat funktioniert. Hier sind meine Fragment Code, manifestieren und Abhängigkeiten Ich verwende:Google Maps V2-Standort, der null zurückgibt

Java-Code

public class MapFragment extends SupportMapFragment implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     GoogleMap.OnInfoWindowClickListener, 
     GoogleMap.OnMapLongClickListener, 
     GoogleMap.OnMapClickListener, 
     GoogleMap.OnMarkerClickListener { 
    private GoogleApiClient mGoogleApiClient; 
    private Location mCurrentLocation; 
    private LocationRequest mLocationRequest; 

    private final int[] MAP_TYPES = { GoogleMap.MAP_TYPE_SATELLITE, 
      GoogleMap.MAP_TYPE_NORMAL, 
      GoogleMap.MAP_TYPE_HYBRID, 
      GoogleMap.MAP_TYPE_TERRAIN, 
      GoogleMap.MAP_TYPE_NONE }; 
    private int curMapTypeIndex = 0; 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     setHasOptionsMenu(true); 

     mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 


     initListeners(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     Log.d("mCurrentLocation",mCurrentLocation +""); 
     initCamera(mCurrentLocation); 

    } 

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

    private void initListeners() { 
     getMap().setOnMarkerClickListener(this); 
     getMap().setOnMapLongClickListener(this); 
     getMap().setOnInfoWindowClickListener(this); 
     getMap().setOnMapClickListener(this); 
    } 

    private void removeListeners() { 
     if(getMap() != null) { 
      getMap().setOnMarkerClickListener(null); 
      getMap().setOnMapLongClickListener(null); 
      getMap().setOnInfoWindowClickListener(null); 
      getMap().setOnMapClickListener(null); 
     } 
    } 
    private void initCamera(Location location) { 
     if (location != null){ 
      CameraPosition position = CameraPosition.builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude())) 
        .zoom(16f) 
        .bearing(0.0f) 
        .tilt(0.0f) 
        .build(); 

      getMap().animateCamera(CameraUpdateFactory.newCameraPosition(position), null); 

      getMap().setMapType(MAP_TYPES[curMapTypeIndex]); 
      getMap().setTrafficEnabled(true); 
      getMap().setMyLocationEnabled(true); 
      getMap().getUiSettings().setZoomControlsEnabled(true); 
     } else { 
      Log.d("hello","hello"); 
     } 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     if(mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     //handle play services disconnecting if location is being constantly used 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     //Create a default location if the Google API Client fails. Placing location at Googleplex 
     mCurrentLocation = new Location(""); 
     mCurrentLocation.setLatitude(37.422535); 
     mCurrentLocation.setLongitude(-122.084804); 
     initCamera(mCurrentLocation); 
    } 


} 

Manifest Erlaubnis

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Abhängigkeiten

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.android.support:design:25.3.1' 
    compile 'com.android.support:recyclerview-v7:22.2.0' 
    compile 'com.google.android.gms:play-services-maps:7.8.0' 
    compile 'com.google.android.gms:play-services-location:7.8.0' 
} 
+0

https://stackoverflow.com/questions/25017764/play-location-services-getlastlocation-returns-null – AndroidHacker

+0

keine Lösung in diesem Link –

+0

Ich glaube, Sie haben nicht vollständige Lösungen von mir zur Verfügung gestellt. – AndroidHacker

Antwort

0

Es ist ein Fehler !!

Sie können null Werte beim Abrufen von Standortdaten finden.

Zunächst müssen wir die GoogleApiClient-Klasse verwenden.

Zweitens brauchen wir GoogleApiClient.ConnectionCallbacks zu implementieren, GoogleApiClient.OnConnectionFailedListener

Drittens brauchen wir seine onConnected(), onConnectionSuspended() und onConnectionFailed() -Methode außer Kraft zu setzen.

Neben oben erwähnten notwendigen Einsatz von onLocationChanged()

machen Sie Standortdaten erhalten, wann immer es in OnLocationChanged() zur Verfügung steht.

Hoffe es klärt.