2016-04-07 2 views
-1

mon code lance une erreur de pointeur nul et plante l'application complète. J'ai vérifié avec l'ID de bouton c'est correct mais ne peux pas trouver une erreur. J'essaie d'allouer cette fonction à un bouton. Fondamentalement, je veux accéder à la longitude actuelle de la latitude de l'emplacement et afficher un marqueur là-bas. Donc, si u ont un code alternatif aussi feraerreur de pointeur null dans 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

afficher les journaux d'erreur/crash –

+0

post la pile trace – Sibidharan

+1

encore une autre ... 'LocationManager.getLastKnownLocation' peut retourner null et il renvoie null dans votre cas – Selvin

Répondre

1

Lieu de lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); pourrait être nulle.

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 il a travaillé avec cette –