如何在片段中使用PlaceAutoCompleteFragment小部件 - Android?

时间:2016-05-19 11:15:42

标签: android android-layout android-fragments android-studio

我在片段中使用PlaceAutoCompleteFragment。第一次Fragment(放置PlaceAutoCompleteFragment的应用程序片段)打开时,它就像魅力一样正常。但是,然后我第二次按下按钮打开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中使用它,它可以正常工作。

4 个答案:

答案 0 :(得分:1)

不要将PlaceAutocompleteFragment直接添加到片段布局中。您需要动态添加PlaceAutocompleteFragment并在onDestroyView()上删除它。

<强> layout.xml

        <LinearLayout
            android:id="@+id/placeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">
        </LinearLayout>

添加PlaceAutocompleteFragment

  PlaceAutocompleteFragment autocompleteFragment=new PlaceAutocompleteFragment();
    FragmentManager fragmentManager = getActivity().getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.placeLayout,autocompleteFragment);
    fragmentTransaction.commit();

将其删除onDestroy

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (getActivity() != null) {
        FragmentManager fragmentManager = getActivity().getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(autocompleteFragment);
        fragmentTransaction.commit();
    }

}

答案 1 :(得分:0)

如果有效,请尝试此操作:

View view;
public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
          if(view == null) {
            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) {

            }
        });
}

答案 2 :(得分:0)

您应该提供有关布局的更多背景信息。异常说明:&#34;重复ID&#34;你确定你没有两次使用相同的id吗?

答案 3 :(得分:0)

尝试在onDestroyView()

中删除片段
@Override
public void onDestroyView() {
    super.onDestroyView();
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(mPlaceAutocompleteFragment);
    ft.commit();
}