2016-04-07 8 views
-1

mein Code wirft null Zeigerfehler und Absturz der gesamten App. Ich habe mit Button ID seine Richtigkeit überprüft, kann aber keinen Fehler finden. Ich versuche diese Funktion einer Schaltfläche zuzuordnen. Grundsätzlich möchte ich auf den Längengrad des aktuellen Längengrads zugreifen und dort einen Marker anzeigen. Also, wenn u irgendeine alternativen Code hat auch wird es tunNull-Zeiger-Fehler in android Studio

public void onPing(View view) { 
    if (view.getId() == R.id.button2) { 
     mMap.clear(); 
     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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; 
     } 
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     double longitude = location.getLongitude(); 
     double latitude = location.getLatitude(); 
     subLoc = new LatLng(latitude, longitude); 
     mMap.addMarker(new MarkerOptions().position(subLoc).title("Current Position")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(subLoc)); 
+0

schreiben die Fehler/Absturz Protokolle –

+0

die Stack-Trace – Sibidharan

+1

noch einen weiteren Posten ... 'LocationManager.getLastKnownLocation' null zurückkehren und es kehrt in Ihrem Fall null – Selvin

Antwort

1

Lage von lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); null sein könnte.

if(location != null){ 
     double longitude = location.getLongitude(); 
     double latitude = location.getLatitude(); 
     subLoc = new LatLng(latitude, longitude); 
     mMap.addMarker(new MarkerOptions().position(subLoc).title("Current Position")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(subLoc)); 
} 
+0

hey thnx es mit diesem gearbeitet –