我正在一个 Fragment 中使用 PlaceAutoCompleteFragment
。第一次打开包含 PlaceAutoCompleteFragment
的应用 Fragment 时,它能够很好地工作。但是,当我第二次点击按钮打开 Fragment 时,就会崩溃,并出现以下错误。它只能正常运行一次。
FATAL EXCEPTION:
android.view.InflateException: Binary XML file line #64: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #64: Duplicate id 0x7f0d0094, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
这是我如何使用它:
SupportPlaceAutocompleteFragment placeAutocompleteFragment = (SupportPlaceAutocompleteFragment) getActivity().getSupportFragmentManager().
findFragmentById(R.id.place_autocomplete_fragment);
placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
System.out.println(place.getAddress());
}
@Override
public void onError(Status status) {
}
});
错误在onCreateDialog
方法的这一行,其中正在进行布局填充:
View view = inflater.inflate(R.layout.add_geofence_layout, null);
XML:
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment" />
想知道为什么这段代码只能运行一次?
DialogFragment类:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.add_geofence_layout, null);
viewHolder = new ViewHolder();
viewHolder.initializeView(view);
viewHolder.placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
System.out.println(place.getAddress());
}
@Override
public void onError(Status status) {
}
});
}
注意:我正在使用一个PlaceautoCompleteFragment
在一个DialogFragment
中。如果我在一个Activity中使用它,它可以正常工作。