2017-05-29 28 views
0

我試圖使用Google的Place自動填充(https://developers.google.com/places/android-api/autocomplete)在我的應用中實現片段。但是我從這個片段到另一個片段中導航,然後回來這個片段如何將Google的PlaceAutoCompleteFragment實現爲擴展片段的類

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)中的答案,但我找不到任何解決方案此地方Aut ocomplete片段

+0

給你自己的'PlaceAutoComplete'一個id。 – tahsinRupam

+0

@tahsinRupam你是什麼意思?任何例子? –

+0

可能這是因爲你在兩個不同的'PlaceAutoComplete'片段中使用了相同的id。 – tahsinRupam

回答

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(),因爲微件是另一片段(我的導航片段)的內部被充氣的片段從充氣谷歌我PlaceAutocompleteFragment插件。

+0

雖然這個鏈接可能回答這個問題,但最好包含基本部分的答案,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/17139659) – MikeT

相關問題