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

标签 java android location

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

我正在按照谷歌的开发人员指南获取我的当前位置,并且我想将其存储到 myLocation(全局变量)。我已成功获取当前位置,但它并未永久存储在 myLocation 中。在 onSuccess() 方法之后,myLocation 变为 null。

公共(public)类 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,但在日志中发现它为空。 日志 - I/myLocation:位置[网络 0.000000,0.000000 acc=??? t=?!?等=?!?] 请帮助我并提前致谢。

最佳答案

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

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

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

关于java - 为什么即使 myLocation 是全局变量,当 onSuccess() 终止时 myLocation 也会变为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54597878/

相关文章:

android - 在 Android 中构建一个无限滚动的类似日历的 View

android - 在 recyclerview OnBindViewHolder 方法中为每一行创建 POJO 对象

android - 我怎样才能像谷歌地图一样在 Android 中获得连续的位置更新?

java - 只有在通过 java 完成进程后,我的代码才会继续

java - Ant 脚本从多个模块运行单元测试,如果测试失败则构建失败

android - 一个 onActivityResult 中的两个扫描事件

javascript - 单击按钮时获取用户的当前位置

gradle - 位置信息包出现错误。怎么修?

java - Kryonet 序列化错误

java - Android View 添加 child