-1

Когда я пытаюсь добавить пользовательский маркер он бросает NullPointerException,андроид нулевого указатель исключения при добавлении пользовательских маркеров

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference 

Вот моя карта activity

public class GoogleMapActivity extends AppCompatActivity 
     { 
     double latitude, longitude; 
     GoogleMap map; 


     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_google_map); 
     SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    private void getCurrentLocation() 
    { 
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
     if (location != null) { 
      //Getting longitude and latitude 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
      moveMap(); 
     } 
     } 
    private void moveMap() 
    { 
     LatLng latLng = new LatLng(latitude, longitude); 

      map.addMarker(new MarkerOptions() 
        .position(latLng) 
        .draggable(true) 
        .title("Current Location")); 

      map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
      map.animateCamera(CameraUpdateFactory.zoomTo(15)); 

     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.googlemap, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_search) { 
      getCurrentLocation(); 
      moveMap(); 

     } 
     return super.onOptionsItemSelected(item); 

    } 
    } 

Как я могу исправить эту проблему?

ответ

0
private void getCurrentLocation() 
{ 
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
    if (location != null) { 
     moveMap(location); 
    } 
    } 
private void moveMap(Location location) 
{ 
    longitude = location.getLongitude(); 
    latitude = location.getLatitude(); 
    LatLng latLng = new LatLng(latitude, longitude); 
    map.addMarker(new MarkerOptions() 
       .position(latLng) 
       .draggable(true) 
       .title("Current Location")); 

    map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    map.animateCamera(CameraUpdateFactory.zoomTo(15)); 
} 

У вас нет широты и долготы в рамках функции вы пытаетесь получить доступ в.

и вы также не выполняют функцию onMapReady(). Похоже, что многое нарушено.

Пример: https://github.com/googlemaps/android-samples/blob/master/tutorials/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java

public class MapsMarkerActivity extends AppCompatActivity 
     implements OnMapReadyCallback { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Retrieve the content view that renders the map. 
     setContentView(R.layout.activity_maps); 
     // Get the SupportMapFragment and request notification 
     // when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    /** 
    * Manipulates the map when it's available. 
    * The API invokes this callback when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user receives a prompt to install 
    * Play services inside the SupportMapFragment. The API invokes this method after the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     // Add a marker in Sydney, Australia, 
     // and move the map's camera to the same location. 
     LatLng sydney = new LatLng(-33.852, 151.211); 
     googleMap.addMarker(new MarkerOptions().position(sydney) 
       .title("Marker in Sydney")); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^