2017-05-29 55 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