onReadyMap方法未被调用

时间:2015-08-28 19:59:20

标签: android google-maps

我正试图在GoogleApiClient活动启动时借助Map获取上次知道的位置信息。我按照链接中的指南进行了操作:

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

它有效,但应用程序有时会崩溃,我收到以下错误。

我试图将mGoogleApiClient.connect()放在onReadyMap()中,但在我的情况下从不调用此方法。

我该如何解决?

错误:

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)

地图活动:

   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);                

            }

        }
    }

当我以这种方式调用mGoogleApiClient.connect();时,在我的情况下没有调用onMapReady()。

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

1 个答案:

答案 0 :(得分:1)

你在哪里隐藏你的onMapReady函数?它不在你的代码中 这是您需要的正确实现:

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);
        }
    }
}