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

时间:2016-12-27 13:20:25

标签: android android-fragments inflate-exception

我在PlaceAutoCompleteFragment内使用Fragment。第一次当Fragment(放置PlaceAutoCompleteFragment的App片段)打开时,它就像魅力一样正常。

但是,然后第二次按下按钮打开Fragment它崩溃时出现以下错误。它只能工作一次。

致命的例外:

  

android.view.InflateException:二进制XML文件行#64:错误   膨胀类片段引起:   java.lang.IllegalArgumentException:二进制XML文件行#64:   重复ID为0x7f0d0094,标记为null或父ID为0xffffffff   另一个片段   com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

我正在使用此搜索位置

public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.parkingview_fragment, container, false);

            mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map_parkingview);
            mapFragment.getMapAsync(this);

          // For Search location
          PlaceAutocompleteFragment  autocompleteFragment = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

                    autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_fragment_parkingview);
            }

        autocompleteFragment.setOnPlaceSelectedListener(this);
        autocompleteFragment.setHint("Search a Location");
//        autocompleteFragment.setBoundsBias(BOUNDS_MOUNTAIN_VIEW);

        return view;

    }

XML:

 <fragment
        android:id="@+id/place_fragment_parkingview"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

2 个答案:

答案 0 :(得分:4)

声明你的片段动态地解决问题。

<FrameLayout android:id="@+id/fragment_content" android:layout_width="match_parent" android:layout_height="wrap_content" />

更新你的片段onCreateView

SupportPlaceAutocompleteFragment autocompleteFragment = new SupportPlaceAutocompleteFragment(); android.support.v4.app.FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fragment_content, autocompleteFragment); ft.commit();

答案 1 :(得分:2)

我正在使用-

<fragment
        android:id="@+id/location_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/plain_border"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />

在我的片段布局文件中并使用-

对其进行初始化
PlaceAutocompleteFragment locationAutocompleteFragment = (PlaceAutocompleteFragment)
        getActivity().getFragmentManager()
                .findFragmentById(R.id.location_autocomplete_fragment);

我切换到了-

<fragment
        android:id="@+id/location_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/plain_border"
        android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment" />

并通过-

进行初始化
SupportPlaceAutocompleteFragment locationAutocompleteFragment = (SupportPlaceAutocompleteFragment)
            getChildFragmentManager()
                    .findFragmentById(R.id.location_autocomplete_fragment);