benz
benz

Reputation: 725

onReadyMap method is not being called

I am trying to get to last known Location with the aid of GoogleApiClient just when the Map activity launches. I followed the guide in the link:

https://developer.android.com/training/location/retrieve-current.html

It works but the app craches sometime and I am getting the error below.

I tried to put mGoogleApiClient.connect() inside the onReadyMap() but this method is never called in my case.

How can I fix it?

Error:

08-28 16:59:02.036: E/AndroidRuntime(2928): FATAL EXCEPTION: main
08-28 16:59:02.036: E/AndroidRuntime(2928): Process: com.bustracker, PID: 2928
08-28 16:59:02.036: E/AndroidRuntime(2928): 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
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.bustracker.Map.onConnected(Map.java:494)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.internal.zzj.zzg(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.api.zze.zzmH(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.api.zze.onConnected(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.api.zzg$2.onConnected(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.internal.zzi$zzg.zznO(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.internal.zzi$zza.zzc(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.internal.zzi$zza.zzr(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.internal.zzi$zzc.zznQ(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.google.android.gms.common.internal.zzi$zzb.handleMessage(Unknown Source)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at android.os.Looper.loop(Looper.java:145)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at android.app.ActivityThread.main(ActivityThread.java:5944)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at java.lang.reflect.Method.invoke(Native Method)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at java.lang.reflect.Method.invoke(Method.java:372)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
08-28 16:59:02.036: E/AndroidRuntime(2928):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Map activity:

   public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{
        GoogleMap map;
        GoogleApiClient mGoogleApiClient;
        Location mLastLocation;
        Marker myLocatMarker;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map);
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            buildGoogleApiClient();
            mGoogleApiClient.connect();
    }
    private boolean initMap() {
        if (map == null) {
            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            map = mapFrag.getMap();
        }
        return (map != null);
    }
        @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);      
            ........
            if (initMap()) {
                gotoLocation(id, latitude, longitude, route_dirc);                                              

            } else {
                Toast.makeText(this, "Map not avialable", Toast.LENGTH_SHORT)
                        .show();
            }
    }

        protected synchronized void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
             System.out.println("ABC buildGoogleApiClient map was invoked: ");
        }
        @Override
        public void onConnected(Bundle arg0) {
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            if (mLastLocation != null) {
                 double lng = mLastLocation.getLongitude();
                 double lat = mLastLocation.getLatitude();

                if(myLocatMarker != null){
                    myLocatMarker.remove();
                }
                 LatLng ll = new LatLng(lat, lng);
                 MarkerOptions markerOpt = new MarkerOptions().title("my location")
                         .position(ll)
                         .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
                 System.out.println("ABC onConnected map: "+ lat + " ; " + lng);
                 myLocatMarker = map.addMarker(markerOpt);                

            }

        }
    }

When I call mGoogleApiClient.connect(); in this way, onMapReady() is not being invoked in my case.

   @Override
public void onMapReady(GoogleMap arg0) {
            mGoogleApiClient.connect(); 
    System.out.println("ABC onMapReady");
}

Upvotes: 0

Views: 279

Answers (1)

Stas Parshin
Stas Parshin

Reputation: 8283

Where are you hiding your onMapReady function? It isn't in your code.
This is correct implementation of what you need:

public class Map extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    GoogleMap map;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    Marker myLocatMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        buildGoogleApiClient();
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
                mGoogleApiClient.connect();
            }
        });
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        System.out.println("ABC buildGoogleApiClient map was invoked: ");
    }

    @Override
    public void onConnected(Bundle arg0) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            double lng = mLastLocation.getLongitude();
            double lat = mLastLocation.getLatitude();

            if (myLocatMarker != null) {
                myLocatMarker.remove();
            }
            LatLng ll = new LatLng(lat, lng);
            MarkerOptions markerOpt = new MarkerOptions().title("my location")
                    .position(ll)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
            System.out.println("ABC onConnected map: " + lat + " ; " + lng);
            myLocatMarker = map.addMarker(markerOpt);
        }
    }
}

Upvotes: 1

Related Questions