首页 > 解决方案 > 我无法在回调函数之外记录变量,该函数在 Android 中分配有其内部的值

问题描述

这是我为变量赋值的函数Place

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

    //check LocationPermission Granted
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }
    else
    {
        fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                try {
                    // This is the place where I am storing my lanlag object
                    place = new LatLng(location.getLatitude(), location.getLongitude());

                }
                catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(), "null refrence in result", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // Here I am trying to log
    Log.i("land and lon", String.valueOf(place.latitude)+" "+String.valueOf(place.longitude));
}

标签: javaandroid

解决方案


推荐阅读