简体   繁体   中英

FusedLocationProvider always returns null Location object

I have tried countless implementation of FusedLocationProviderClient I have found on the internet and using the documentation, but everytime I couldn't get a location and a null object. Here's my final code where I get the location with FusedLocationProviderClient in my main activity. Then I use the Location object in another fragment to show coordinates of the user.

Here's the activity code

    ActivityMainBinding activityMainBinding;
    NavHostFragment navHostFragment;
    NavController navController;
    LocationRequest mLocationRequest;
    Location mLocation;
    FusedLocationProviderClient mFusedLocationClient;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startLocation();

        activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(activityMainBinding.getRoot());
        navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerMain);
        navController = navHostFragment.getNavController();

        activityMainBinding.bottomNavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch(item.getItemId()) {
                    case R.id.menuItemHome:
                        navController.navigate(R.id.homeFragment);
                        break;
                    case R.id.menuItemSearch:
                        navController.navigate(R.id.searchFragment);
                        break;
                    case R.id.menuItemProfile:
                        navController.navigate(R.id.profileFragment);
                        break;
                    case R.id.menuItemSettings:
                        navController.navigate(R.id.settingsFragment);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        navController.navigate(R.id.homeFragment);
        activityMainBinding.bottomNavigation.setSelectedItemId(R.id.menuItemHome);
    }

    public void startLocation() {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (    (ContextCompat.checkSelfPermission( this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
                || ( ContextCompat.checkSelfPermission( this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ) {
            //Location Permission granted
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
            LocationServices.getFusedLocationProviderClient(this).getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(@NonNull Location location) {
                    mLocation = location;
                }
            });
        } else {
            //Request Location Permission
            checkLocationPermission();
        }
    }

    public void stopLocationUpdates() {
        if( mFusedLocationClient != null )
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
    }

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
    private void checkLocationPermission() {
        if (    (ContextCompat.checkSelfPermission( this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
                || ( ContextCompat.checkSelfPermission( this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ) {

            if (    ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    || ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) ) {

                //Request location message for user
                new AlertDialog.Builder(this)
                        .setTitle("Location Permission Needed")
                        .setMessage("This app needs the Location permission, please accept to use location functionality")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //Prompt the user once explanation has been shown
                                ActivityCompat.requestPermissions(MainActivity.this,
                                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION );
                            }
                        })
                        .create()
                        .show();
            } else {
                //request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION );
            }
        }
    }

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSIONS_REQUEST_LOCATION) {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
                }
            }
        }
    }

    LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                if (location != null) {
                    mLocation = location;
                    return;
                }
            }
        }
    };
}

And this is the code in the fragment class where I want to show the coordinates

public class HomeFragment extends Fragment {
    private HomeFragmentBinding homeFragmentBinding;
    private NavController navController;
    private MainViewModel mainViewModel;
    private Location userLocation;
    private MainActivity mainActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainViewModel = new ViewModelProvider(getActivity()).get(MainViewModel.class);
        mainActivity = (MainActivity) getActivity();
        userLocation = mainActivity.mLocation;

        homeFragmentBinding.textDist1.setText("Lat: " + userLocation.getLatitude() + " Long: " + userLocation.getLongitude());
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        homeFragmentBinding = HomeFragmentBinding.inflate(inflater, container, false);
        return homeFragmentBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        navController = Navigation.findNavController(view);

        homeFragmentBinding.textDist1.setText("Lat: " + userLocation.getLatitude() + " Long: " + userLocation.getLongitude());

    }

I also added permissions for ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION in my manifest and

implementation 'com.google.android.gms:play-services-location:19.0.0' in the gradle build.

I get a null pointer exception for null Location object everytime I run the app. The user also doesn't get the popup for giving location access. I manually opened location access from the settings of my android virtual device but nothing changed. I cannot find the issue after countless hours on android documentation and old StackOverflow pages. Thanks for any help.

First, create an instance of the Fused Location Provider Client.

mFusedLocationClient= LocationServices.getFusedLocationProviderClient(this)

Then with that do the location requests.

mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());

I guess the official doc is pretty clear. Refer - https://developer.android.com/training/location/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM