如何在Android中的Fragment中使用PlaceAutoCompleteFragment小部件?

4

我正在一个 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中使用它,它可以正常工作。


分享你用来打开Fragment的代码。 - x0r
我猜你需要在父片段的onDestroy中销毁/移除places片段。 - sanedroid
@PoojaGaikwad 你所说的“destroy/remove”是什么意思?能否详细说明一下。 - Poras Bhardwaj
当一个布局已经在上下文中存在并且您正在尝试再次填充它时,就会发生这种情况。您试过这样做吗?:View视图;如果(view == null){ view = inflater.inflate(R.layout.add_geofence_layout,null);} - sanedroid
让我们在聊天中继续这个讨论 - Poras Bhardwaj
显示剩余5条评论
4个回答

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();
    }

}


0

关于布局,您应该提供更多的上下文信息。异常状态:“重复 id”,您确定没有两次使用相同的 id 吗?


0
尝试在onDestroyView()中移除片段。
@Override
public void onDestroyView() {
    super.onDestroyView();
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(mPlaceAutocompleteFragment);
    ft.commit();
}

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) {

            }
        });
}

尝试过了,亲爱的,但它不起作用。 :) 怎么会这样呢?每次调用此类时,我们都会得到一个空视图,因为视图尚未初始化。对吧? - Poras Bhardwaj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接