【问题标题】:AutocompleteSupportFragment places .getAddress is null. Is getAddress is deprecated?AutocompleteSupportFragment 将 .getAddress 放置为空。 getAddress 是否已弃用?
【发布时间】:2019-02-24 20:29:18
【问题描述】:

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

【问题讨论】:

  • 能否请您发布堆栈跟踪?

标签: android google-maps android-fragments


【解决方案1】:

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

【讨论】:

  • 我想从place API获取地址,所以没有得到
  • 您可以在使用place.getAddress());之前尝试请求地址字段Place.Field.ADDRESS,详情请参阅文档link
【解决方案2】:

修改两个部分:

代码 sn-p 共享的第 1 行,来自: AutocompleteSupportFragment places places = (AutocompleteSupportFragment) 到 : AutocompleteSupportFragment places = (AutocompleteSupportFragment)

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

【讨论】:

    【解决方案3】:

    使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2020-03-30
      • 2019-10-29
      • 2023-03-08
      • 2018-09-12
      相关资源
      最近更新 更多