【问题标题】:How to use PlaceAutoCompleteFragment widget in a Fragment如何在片段中使用 PlaceAutoCompleteFragment 小部件
【发布时间】:2017-05-11 18:55:42
【问题描述】:

我在Fragment 中使用PlaceAutoCompleteFragment。第一次打开片段(其中放置PlaceAutoCompleteFragment 的应用程序片段)时,它像魅力一样工作得很好。

但是,然后我第二次点击按钮打开 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"/>

【问题讨论】:

  • 乔丹的回答是正确的。你应该这样选择它。

标签: android android-fragments inflate-exception


【解决方案1】:

动态声明您的片段,它将解决问题。

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

【讨论】:

【解决方案2】:

此问题是因为您尚未删除之前创建的 PlaceAutoCompleteFragment 实例。

小伙伴们小心点!如果您使用 Jordon 发布的方法,它会起作用,但是片段的视图(例如输入编辑文本和清除按钮)将无法访问,因为尚未创建片段的视图......正确的答案是 REMOVE放入 xml 文件中的片段实例,以编程方式覆盖包含 PlaceAutoCompleteFragment 的片段上的 onDestroyView 方法。

@Override
public void onDestroyView() {    
if (autocompleteFragment != null) {
   android.support.v4.app.FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); 
   fragmentTransaction.remove(autocompleteFragment).commitNowAllowingStateLoss();
}
super.onDestroyView();    
}     

这样,无论何时重新创建 Fragment,它都不会创建 PlaceAutoCompleteFragment 的实例。快乐编码!

【讨论】:

  • 我有这个错误:不兼容的类型:PlaceAutocompleteFragment 无法转换为 Fragment
【解决方案3】:

我正在使用-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多