getCurrentPlace() ничего не возвращает
Я пытаюсь создать приложение, которое использует средство выбора мест и в то же время использует функцию PlaceDetectionClient.getCurrentPlace () Places SDk для получения текущего местоположения.
Однако результат задачи никогда не возвращает допустимый объект Place, ниже приведен мой фрагмент кода:
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkLocationSettings(MainActivity.this);
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.i("Permissions", "Location Permission not granted, request for permission.");
// Should we show an explanation?
PermissionUtil.requestForPermissions(MainActivity.this, listFragment, REQUEST_LOCATION);
}
else if (checkAirPlaneMode(getApplicationContext())) {
Snackbar.make(findViewById(R.id.coordinator_layout), "Air Plane Mode is ON, Please turn off to get location", Snackbar.LENGTH_LONG).show();
}
else if (!checkNetwork()) {
Snackbar.make(findViewById(R.id.coordinator_layout), "Unable to reach network, please check network settings", Snackbar.LENGTH_LONG).show();
}
else {
PlacePicker.IntentBuilder placeBuilder = new PlacePicker.IntentBuilder();
try{
startActivityForResult(placeBuilder.build(MainActivity.this), PLACE_PICKER_REQUEST);
}
catch (GooglePlayServicesNotAvailableException e) {
Log.e(G_PLACE_SERVICE_ERROR, "Google places service not availabe...");
Snackbar.make(findViewById(R.id.coordinator_layout), "Google Play Service is currently unavailable, please check your connections", Snackbar.LENGTH_LONG).show();
}
catch (GooglePlayServicesRepairableException er) {
Log.e(G_PLACE_SERVICE_ERROR, "Google places service may not be installed or up to date...");
Snackbar.make(findViewById(R.id.coordinator_layout), "Please make sure you have installed and make sure Google Play service is up to date", Snackbar.LENGTH_LONG).show();
//TODO: show installation dialog to user
}
Task<PlaceLikelihoodBufferResponse> currentPlaceResult = mPlaceDetectionClient.getCurrentPlace(null);
currentLikelihoodValue = 0;
currentPlaceResult.addOnCompleteListener(new OnCompleteListener<PlaceLikelihoodBufferResponse>() {
@Override
public void onComplete(@NonNull Task<PlaceLikelihoodBufferResponse> task) {
if (task.isSuccessful() && task.getResult() != null) {
PlaceLikelihoodBufferResponse likelyPlaces = task.getResult();
for (PlaceLikelihood placeLikelihood: likelyPlaces) {
newLikelihoodValue = placeLikelihood.getLikelihood();
if (newLikelihoodValue > currentLikelihoodValue) {
currentLikelihoodValue = newLikelihoodValue;
currentPlace = placeLikelihood.getPlace();
}
}
likelyPlaces.release();
} else {
Log.e(CURRENT_PLACE_ERROR, "getCurrentPlace is not working...");
}
}
});
currentLocation = new Location("currentLocation");
currentLocation.setLongitude(currentPlace.getLatLng().longitude);
currentLocation.setLatitude(currentPlace.getLatLng().latitude);
selectedLocation = new Location("selectedLocation");
selectedLocation.setLongitude(selectedPlace.getLatLng().longitude);
selectedLocation.setLatitude(selectedPlace.getLatLng().latitude);
if (selectedLocation.distanceTo(currentLocation) > maxDistance) {
Snackbar.make(findViewById(R.id.coordinator_layout), "Hey, do select nearby places, don't make it up!", Snackbar.LENGTH_LONG).show();
} else {
listFragment.list.add(selectedPlace.getAddress().toString());
listFragment.mAdapter.notifyDataSetChanged();
}
}
}
});
Таким образом, код здесь будет возвращать исключение nullPointerException, потому что currentPlace имеет значение null. Я даже пытался поместить точку отладки в метод onComplete(), но, похоже, он никогда не работает там.
Кто-нибудь может пролить немного света? Заранее спасибо.