我在我的项目中使用AutocompleteSupportFragment
AutocompleteSupportFragment places places = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.place_autocompleteFragment);
places.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
places.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull Place place) {
try {
if ( location_switch.isChecked() ) {
if ( place.getName() != null ) {
destination = place.getName().toString();
destination = destination.replace(" ", "+");
if ( Constants.IS_LOGG_ENABLE ) {
Log.d(Constants.TAG, "Destination Location = " + destination);
Log.d(Constants.TAG, "Place: " + place.getName() + ", Place ID: " + place.getId());
Log.d(Constants.TAG, "Address = " + place.getAddress());
}
getDirection();
} else {
Toast.makeText(DriverMainActivity.this, "Error occurred, please try again.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(DriverMainActivity.this, "Please change your status to ONLINE", Toast.LENGTH_SHORT).show();
}
}catch (Exception ex){
if(Constants.IS_LOGG_ENABLE){
ex.printStackTrace();
}
}
}
完全得到我的地名和身份证,但我需要所选地点的地址
Log.d(Constants.TAG,“Place:”+ place.getName()+“,Place ID:”+ place.getId());
像这样,但我在这一行得到了java.lang.NullPointerException
Log.d(Constants.TAG,“Address =”+ place.getAddress());
您是否考虑在此行places.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
中设置地址,您似乎只设置了ID和名称,但您还想获取未设置为任何值的地址
使用Place.Field.values()获取所有Places值
List<Place.Field> fields = Arrays.asList(Place.Field.values());
// List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);