Java 谷歌地图移动摄像头不';不行-为什么?

Java 谷歌地图移动摄像头不';不行-为什么?,java,android,google-maps,nullpointerexception,Java,Android,Google Maps,Nullpointerexception,此活动用于获取当前用户位置,并每10秒更新一次,问题在于: mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20)); 不起作用,地图显示如下 如果我将这行移到onCreate应用程序崩溃,并显示错误消息: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.

此活动用于获取当前用户位置,并每10秒更新一次,问题在于:

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
不起作用,地图显示如下

如果我将这行移到onCreate应用程序崩溃,并显示错误消息:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
        at com.ENG.Activities.Maps.UserLocation.onCreate(UserLocation.java:56)
我应该如何打开地图并缩放到我的位置

UserLocationActivity

public class UserLocation extends AppCompatActivity implements OnMapReadyCallback,
    LocationListener, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest mLocationRequest;
private Location mLastLocation;
private LatLng latLng;
private double lat, lng;
private Marker mCurrentUserLocationMarker;
private final int UPDATE_LOCATION_TIME = 10000;
private final int FASTEST_LOCATION_TIME = 5000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_location);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_current_location);
    mapFragment.getMapAsync(this);

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}

private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mGoogleApiClient.connect();
}

private synchronized void buildLocationRequest() {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(UPDATE_LOCATION_TIME)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setFastestInterval(FASTEST_LOCATION_TIME);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    this.mMap = googleMap;

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    buildGoogleApiClient();
    mMap.setMyLocationEnabled(true);
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    buildLocationRequest();
}

@Override
public void onConnectionSuspended(int i) {
    buildGoogleApiClient();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Toast.makeText(getApplicationContext(), "Failed to connect", Toast.LENGTH_LONG).show();
    buildGoogleApiClient();
}

@Override
public void onLocationChanged(Location location) {
    this.mLastLocation = location;

    if (mCurrentUserLocationMarker != null) {
        mCurrentUserLocationMarker.remove();
    }

    lat = mLastLocation.getLatitude();
    lng = mLastLocation.getLongitude();
    latLng = new LatLng(lat, lng);
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng)
            .title("Current user Location")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

    mCurrentUserLocationMarker = mMap.addMarker(markerOptions);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
}

}

您确定执行了
onLocationUpdate
功能吗


onCreate
函数调用
moveCamera
时发生崩溃是完全正常的,因为
Camera
对象还不存在。它只在执行
onMapReady
时才存在。

应用程序崩溃也在我调用moveCamera inside onMapReady时,我不确定“已执行”是什么意思,但只有LocationChanged可以工作并给我正确的位置。我的意思是每次测量新位置时触发。在
onMapReady
中对
moveCamera
的调用崩溃时,您是否可以发布收到的错误消息?