【问题标题】:How to implement Google's PlaceAutoCompleteFragment into a class that extends Fragment如何将 Google 的 PlaceAutoCompleteFragment 实现到扩展 Fragment 的类中
【发布时间】:2017-05-29 04:23:21
【问题描述】:

我正在尝试使用 Google 的 Place Autocomplete (https://developers.google.com/places/android-api/autocomplete) 在我的应用中实现片段。但是当我从这个片段导航到另一个片段然后回到这个片段时,我得到了这个错误

 Caused by: java.lang.IllegalArgumentException: Binary XML file line #30: Duplicate id 0x7f0f010f, tag null, or parent id 0x7f0f00c0 with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
                  at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2148)

这是java代码

private PlaceAutocompleteFragment mSearchPAF;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.some_layout, container, false);
    mSearchLocationPAF = (PlaceAutocompleteFragment) parentActivity.getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
}

这是 XML 文件

<RelativeLayout
    android:id="@+id/someRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/some_drawable">
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

我在同一个 java 和 xml 文件中还有另一个地图片段,但我设法按照这篇文章 (Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment) 中的答案解决了这个问题,但我找不到这个地方自动完成片段的任何解决方案

【问题讨论】:

  • 给你自己的PlaceAutoComplete一个id。
  • @tahsinRupam 你这是什么意思?有什么例子吗?
  • 可能这会导致您在两个不同的PlaceAutoComplete 片段中使用相同的 ID。
  • 你在 PlaceAutocomplete 的两个片段中使用相同的 id
  • @Sandeepdhiman 如果你的意思是这个 (android:id="@+id/place_autocomplete_fragment") 那么没有

标签: java android android-fragments google-places-api


【解决方案1】:

我已经按照此处的答案之一自己解决了这个问题 Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

private PlaceAutocompleteFragment mSearchPAF;
@Override
public void onDestroyView() {
    super.onDestroyView();
    PlaceAutocompleteFragment f = (PlaceAutocompleteFragment) getFragmentManager()
                                     .findFragmentById(R.id.place_autocomplete_fragment);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}

【讨论】:

    【解决方案2】:

    我目前正在处理我的项目并使用多个片段实现了我的主要活动(在 Android Studio 中仍然是新手)。在我的导航片段中,我使用了 Google 提供的 googleAutocomplete。

    这里的代码是在 OnCreateView() 中实现的。

    mPlace_Starting = (PlaceAutocompleteFragment)
            this.getChildFragmentManager().findFragmentById(R.id.place_starting);
    
    AutocompleteFilter countryFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(Place.TYPE_COUNTRY)
            .setCountry("PH")
            .build();
    
    mPlace_Starting.setFilter(countryFilter);
    
    mPlace_Starting.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place:
            Log.i(TAG, "Starting Place:" + place.getName());
            Toast.makeText(getActivity(), "Starting Place: " + place.getName(),
                    Toast.LENGTH_SHORT).show();
    
            double mLongitude = place.getLatLng().longitude;
            double mLatitude = place.getLatLng().latitude;
    
            mStartingpoint = new Waypoint(mLongitude,mLatitude);
            mStartpoint = new GeoCoordinate(mLongitude,mLatitude);
        }
        @Override
        public void onError(Status status) {
            //TODO: Handle the Error.
            Log.i(TAG, "An error occured: " + status);
        }
    });

    我使用 getChildFragmentManager() 从 Google 为我的 PlaceAutocompleteFragment 小部件充气,因为小部件是要在另一个片段(我的导航片段)中充气的片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2018-04-29
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      相关资源
      最近更新 更多