添加自定义标记时,android null指针异常

时间:2017-01-07 17:34:47

标签: java android android-fragments nullpointerexception google-maps-markers

当我尝试添加自定义标记时,它会抛出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);

    }
  }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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));
    }
}