如何将Google的PlaceAutoCompleteFragment实现到扩展Fragment的类中

时间:2017-05-29 04:23:21

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

我尝试使用Google的地方自动填充功能(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)中的答案来解决它,但我无法找到此地方自动填充的任何解决方案片段

2 个答案:

答案 0 :(得分:2)

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

{{1}}

答案 1 :(得分:1)

我目前正在开发我的项目,并使用多个片段(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小部件,因为小部件是要在另一个片段(我的导航片段)中充气的片段。

相关问题