0

In play-services-maps 9.4.0 version of the API, They replaced GoogleMap.OnCameraChangeListener() with three camera listeners :

1.GoogleMap.OnCameraMoveStartedListener

2.GoogleMap.OnCameraMoveListener

3.GoogleMap.OnCameraIdleListener

I want to change my following code to work with play-services-maps 9.4.0 version of the API,as I get nullpointerException on this.How should I do that?

googlemap.setOnCameraChangeListener(new OnCameraChangeListener() {
// try{

@Override
public void onCameraChange(CameraPosition arg0) {

    LatLongDetails.user_latitude = arg0.target.latitude;
    LatLongDetails.user_longitude = arg0.target.longitude;
    if(LatLongDetails.user_latitude!=0||LatLongDetails.user_longitude!=0)
    new ConversionTaskLatLonLoc(contextFix).execute();
     else{
          googlemap.clear();
            Toast.makeText(context,"error occured, please_wait", Toast.LENGTH_SHORT)
                    .show();
            user_latlongobj = findLocation();
            animatecamera(user_latlongobj);
            getCabData();
       }
    MapUtil.clearMarkers(googlemap);
    MapUtil.dropPin(googlemap, arg0.target.latitude,
            arg0.target.longitude, R.drawable.marker,
            LoginDetails.Address).showInfoWindow();

    }
});

Here is logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnCameraChangeListener(com.google.android.gms.maps.GoogleMap$OnCameraChangeListener)' on a null object reference
Authentication failed on the server.
Google Maps Android API: Authorization failure.  Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map
In the Google Developer Console (https://console.developers.google.com)
Ensure that the "Google Maps Android API v2" is enabled.
Ensure that the following Android Key exists:
API Key: ************************************
Android Application (<cert_fingerprint>;<package_name>): **************
Hadi Samadbin
  • 237
  • 3
  • 17

1 Answers1

2

googlemap is null. Do not call setOnCameraChangeListener() until after you have assigned a value to googlemap. onMapReady() would be a likely candidate, as I demonstrate in this sample app:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.mapsv2.camera;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveCanceledListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveStartedListener;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends AbstractMapActivity implements
    OnMapReadyCallback, OnInfoWindowClickListener,
  OnCameraMoveStartedListener,
  OnCameraMoveListener,
  OnCameraMoveCanceledListener,
  OnCameraIdleListener {
  private boolean needsInit=false;
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (readyToGo()) {
      setContentView(R.layout.activity_main);

      MapFragment mapFrag=
          (MapFragment)getFragmentManager().findFragmentById(R.id.map);

      if (savedInstanceState == null) {
        needsInit=true;
      }

      mapFrag.getMapAsync(this);
    }
  }

  @Override
  public void onMapReady(final GoogleMap map) {
    this.map=map;

    if (needsInit) {
      CameraUpdate center=
          CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                                                   -73.98180484771729));
      CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

      map.moveCamera(center);
      map.animateCamera(zoom);
    }

    addMarker(map, 40.748963847316034, -73.96807193756104,
              R.string.un, R.string.united_nations);
    addMarker(map, 40.76866299974387, -73.98268461227417,
              R.string.lincoln_center,
              R.string.lincoln_center_snippet);
    addMarker(map, 40.765136435316755, -73.97989511489868,
              R.string.carnegie_hall, R.string.practice_x3);
    addMarker(map, 40.70686417491799, -74.01572942733765,
              R.string.downtown_club, R.string.heisman_trophy);

    map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
    map.setOnInfoWindowClickListener(this);

    map.setOnCameraMoveStartedListener(this);
    map.setOnCameraMoveListener(this);
    map.setOnCameraMoveCanceledListener(this);
    map.setOnCameraIdleListener(this);
  }

  @Override
  public void onInfoWindowClick(Marker marker) {
    Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
  }

  @Override
  public void onCameraIdle() {
    CameraPosition position=map.getCameraPosition();

    Log.d("onCameraIdle",
      String.format("lat: %f, lon: %f, zoom: %f, tilt: %f",
        position.target.latitude,
        position.target.longitude, position.zoom,
        position.tilt));
  }

  @Override
  public void onCameraMoveCanceled() {
    CameraPosition position=map.getCameraPosition();

    Log.d("onCameraMoveCanceled",
      String.format("lat: %f, lon: %f, zoom: %f, tilt: %f",
        position.target.latitude,
        position.target.longitude, position.zoom,
        position.tilt));
  }

  @Override
  public void onCameraMove() {
    CameraPosition position=map.getCameraPosition();

    Log.d("onCameraMove",
      String.format("lat: %f, lon: %f, zoom: %f, tilt: %f",
        position.target.latitude,
        position.target.longitude, position.zoom,
        position.tilt));
  }

  @Override
  public void onCameraMoveStarted(int i) {
    CameraPosition position=map.getCameraPosition();

    Log.d("onCameraMoveStarted",
      String.format("lat: %f, lon: %f, zoom: %f, tilt: %f",
        position.target.latitude,
        position.target.longitude, position.zoom,
        position.tilt));
  }

  private void addMarker(GoogleMap map, double lat, double lon,
                         int title, int snippet) {
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
      .title(getString(title))
      .snippet(getString(snippet)));
  }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • if google map is null may be the problem is in google Api key.whats your Idea? also I completed the log cat u can check it. – Hadi Samadbin Nov 14 '16 at 18:04
  • 1
    The `GoogleMap` object received on the `onMapReady` method will not be `null`. You may receive a `null` `GoogleMap` if you are using the deprecated `getMap` method. This answer is a good example of how things must be done – antonio Nov 14 '16 at 20:21
  • After using your code still I have problem I opend another question http://stackoverflow.com/questions/40607001/nullpointerexception-when-trying-to-place-marker-on-google-map-v2?noredirect=1#comment68448648_40607001 – Hadi Samadbin Nov 15 '16 at 10:07