【问题标题】:android null pointer exception when adding custom marker [duplicate]添加自定义标记时的android空指针异常[重复]
【发布时间】:2017-05-22 07:41:34
【问题描述】:

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

    }
  }

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:
    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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 2013-03-04
      • 2022-07-07
      • 2013-04-13
      • 2019-11-17
      • 2018-10-06
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多