即使 myLocation 是全局变量,为什么当 onSuccess() 终止时 myLocation 变为 null?

Why myLocation becomes null when onSuccess() gets terminated even when myLocation is a global variable?

当 onSuccess() 方法终止时,myLocation 值变为空。我是 android 应用程序开发的新手。我想将当前位置的值永久存储在 myLocation 中。

我正在按照 google 的开发人员指南获取我的当前位置,我想将它存储到 myLocation(全局变量)。我已成功获得我的当前位置,但它没有永久存储在 myLocation 中。在 onSuccess() 方法之后,myLocation 变为空。

public class MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback {

private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationClient;
SupportMapFragment mapFragment;
Location myLocation;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    myLocation = new Location(LocationManager.NETWORK_PROVIDER);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
     mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }
    else{
        mapFragment.getMapAsync(this);

    }
}



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

    // Add a marker in Sydney and move the camera
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }

    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location currentlocation) {
                    myLocation=currentlocation;
                    // Got last known location. In some rare situations this can be null.
                    if (currentlocation != null) {

                        LatLng sydney = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
                        mMap.addMarker(new MarkerOptions().position(sydney).title("My Location"));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


                    }
                }
            });
    Log.i("myLocation",myLocation.toString());


}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mapFragment.getMapAsync(this);
        } else {
            Toast.makeText(getApplicationContext(), "Please provide permission", Toast.LENGTH_SHORT).show();
        }
    }


}

}

myLocation 应该等于 currentlocation 但在日志中发现它为 null.. 日志 - I/myLocation:位置[网络 0.000000,0.000000 acc=??? t=?!? et=?!?] 请帮助我并提前致谢。

日志在 onSuccess() 之前被调用,因为它是 Listener。你应该做的是呼吁

Log.i("myLocation",myLocation.toString());

在你的 onSuccess() 中,之后你就有了正确的值。