com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)'在null对象引用上

时间:2015-08-28 14:19:37

标签: android google-maps

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

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

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

我该如何解决?

错误:

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

地图活动:

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

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

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

        }

    }
}

2 个答案:

答案 0 :(得分:1)

我认为你会在onConnected

之前开始onMapReady回调

尝试在mGoogleApiClient.connect()回调中调用onMapReady,以确保地图不为空。

如果您不想使用异步正确,只需使用同步方式获取地图。

删除此mapFragment.getMapAsync(this);
并添加此map = mapFragment.getMap();

请确保您有字段GoogleMap map,因为您将其隐藏在代码中。

或者这是另一个版本

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

答案 1 :(得分:0)

这是因为您的地图对象未定义和初始化。将其全局声明为

    GoogleMap map;
并将其初始化如下..
        SupportMapFragment supportMapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        map = mapFragment.getMap();