addOnCompleteListener is Unchecked and crashes app

两盒软妹~` 提交于 2020-04-30 10:02:23

问题


I have this code:

// Using this method to center MyLocation as default location when app is started
    private void getDeviceLocation() {
        Log.d(TAG, "getDeviceLocation: getting the devices current location");
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    try {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //Request Location Permission
            checkLocationPermission();
        } else {
        final Task location = mFusedLocationClient.getLastLocation();
            location.addOnCompleteListener(this, new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "onComplete: found location!");
                        Location mLastLocation = (Location) task.getResult();

                        // mLastLocation = location;
                        if (mCurrentLocationMarker != null) {
                            mCurrentLocationMarker.remove();
                        }

                        LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.position(latLng);
                        markerOptions.title("My position");
                        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.emptyicon));
                        mCurrentLocationMarker = mMap.addMarker(markerOptions);
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));

                    } else {
                        Log.d(TAG, "onComplete: current location is null");
                        Toast.makeText(MapsActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    } catch (SecurityException e) {
        Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage());
    }

the line: location.addCompleteListener is marked as unchecked by Android Studio and it crashes the app. So I have to make it "checked" in somehow. This happened after the latest update patch for Android Studio.

How do I solve this?


回答1:


Try changing:

final Task location = mFusedLocationClient.getLastLocation();
    location.addOnCompleteListener(this, new OnCompleteListener() {

to this:

final Task<Location> location = mFusedLocationClient.getLastLocation();
    location.addOnCompleteListener(this, new OnCompleteListener<Location>() {



回答2:


I would comment if I had 50+ rep.

Anyway, you can continue using

.addOnCompleteListener(new OnCompleteListener<Location>(){
});

by, adding to what @nope4561759 said, adding <Location> to Task argument on onComplete() overridden method like so: public void onComplete(@NonNull Task<Location> task) {…}




回答3:


 private void getDeviceLocation() {
    Log.d(TAG, "getDeviceLocation: getting the devices current location");
    FusedLocationProviderClient mfusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
    try {
        if (mLocationPermissionGranted) {
            final Task<Location> location = mfusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(this, new OnCompleteListener<Location>() {

                @Override
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "onComplete: found location");
                        try {
                            Location currentLocation = (Location) task.getResult();
                            //globalLatitude = currentLocation.getLatitude();
                            //globalLongitude = currentLocation.getLongitude();
                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(), "Please turn on your GPS", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Log.d(TAG, "onComplete: Location not found");
                        Toast.makeText(MainActivity.this, "Cannot find current location", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    } catch (SecurityException e) {
        Log.e(TAG, "getDeviceLocation: Security Exception" + e.getMessage());
    }
}

addOnCompleteListener is Unchecked and crashes app solution.



来源:https://stackoverflow.com/questions/49782739/addoncompletelistener-is-unchecked-and-crashes-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!