未选中addOnCompleteListener并导致应用崩溃 [英] addOnCompleteListener is Unchecked and crashes app
问题描述
我有此代码:
// 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());
}
以下行:location.addCompleteListener被Android Studio标记为未选中,这会使应用崩溃.因此,我必须以某种方式使其选中". 这是在针对Android Studio的最新更新补丁之后发生的.
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.
我该如何解决?
推荐答案
如果我有50个以上的代表,我会发表评论.
I would comment if I had 50+ rep.
无论如何,您可以继续使用
Anyway, you can continue using
.addOnCompleteListener(new OnCompleteListener<Location>(){
});
通过添加到@ nope4561759所表示的内容,将<Location>
添加到onComplete()重写方法的Task参数上,如下所示:public void onComplete(@NonNull Task<Location> task) {…}
by, adding to what @nope4561759 said, adding <Location>
to Task argument on onComplete() overridden method like so: public void onComplete(@NonNull Task<Location> task) {…}
这篇关于未选中addOnCompleteListener并导致应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!