AutocompleteSupportFragment将.getAddress设置为null。是不赞成使用getAddress吗?

时间:2019-02-24 20:29:18

标签: android google-maps android-fragments

我在项目中使用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();
        }
    }
}

完全可以为我获取地点名称和ID,但我需要该所选地点的地址

  

Log.d(Constants.TAG,“位置:” + place.getName()+“,位置ID:” +   place.getId());

像这样,但是我在这行上得到了java.lang.NullPointerException

  

Log.d(Constants.TAG,“地址=” + place.getAddress());

3 个答案:

答案 0 :(得分:0)

您是否考虑过在此行places.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));中设置地址,您似乎只设置了ID和名称,但是还希望获得未设置任何值的地址

答案 1 :(得分:0)

使用 Place.Field.values()获取所有Places值

onload

答案 2 :(得分:0)

改变两个部分:

共享代码片段的第 1 行,来自: AutocompleteSupportFragment places places = (AutocompleteSupportFragment) 到 : AutocompleteSupportFragment places = (AutocompleteSupportFragment)

代码片段的第 3 行共享,来自: 这:places.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME)); 到 `places.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.ADDRESS));