将 PlaceAutocompleteFragment 添加到片段会引发错误
Adding PlaceAutocompleteFragment to fragment throws error
我已经在 activity 中实现了 PlaceAutocompleteFragment 并且运行成功。但是如何在 android 中的片段中实现相同的内容?
我已经实现了这样的 placeautocomplete 片段
PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);
我得到的错误是
Incovertible types;cannot cast 'android.support.v4.app.Fragment' to com.google.android.gms.location.PlaceAutocompleteFragment '.
XML 布局是
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="0dp">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Place"
android:background="#fff"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</android.support.v7.widget.CardView>
提前致谢
您必须在 XML 布局文件中使用以下内容:
<fragment
android:id="@+id/place_autocomplete_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
注意完整的类名和 ID,您的 ID 不匹配。 (place_autocomplete_fragment1)
像这样使用getActivity()
。
PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);
片段id应该是android:id="@+id/place_autocomplete_fragment1"
<fragment
android:id="@+id/place_autocomplete_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"/>
此错误的另一个合理原因是您必须将 Fragment
class 从 android.support.v4.app.Fragment
扩展到 PlaceAutoCompleteFragment
,从而扩展 Fragment
class 来自 com.google.android.gms.location.PlaceAutocompleteFragment
。
为避免这种情况,您 should/can 使用 SupportPlaceAutoCompleteFragment
而不是您正在使用。
SupportPlaceAutocompleteFragment autocompleteFragment1 = (SupportPlaceAutocompleteFragment)
getActivity.getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);
使用此功能,您的自动完成小部件也可以在较低版本的设备上使用。
- 在您的布局 xml
中更改 SupportPlaceAutocompleteFragment 而不是 PlaceAutocompleteFragment
fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
更改 getChildFragmentManager() 而不是 getFragmentManager() 并使用 SupportPlaceAutocompleteFragment 而不是 PlaceAutocompleteFragment 在你的 java 文件中,.
SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
这是 Kotlin 中的解决方案。这专门用于如果您想在片段中启动自动完成片段。
val autocompleteFragment = childFragmentManager.findFragmentById(R.id.autocomplete_support_fragment) as AutocompleteSupportFragment?
<fragment
android:id="@+id/autocomplete_support_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
我已经在 activity 中实现了 PlaceAutocompleteFragment 并且运行成功。但是如何在 android 中的片段中实现相同的内容? 我已经实现了这样的 placeautocomplete 片段
PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);
我得到的错误是
Incovertible types;cannot cast 'android.support.v4.app.Fragment' to com.google.android.gms.location.PlaceAutocompleteFragment '.
XML 布局是
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="0dp">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Place"
android:background="#fff"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</android.support.v7.widget.CardView>
提前致谢
您必须在 XML 布局文件中使用以下内容:
<fragment
android:id="@+id/place_autocomplete_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
注意完整的类名和 ID,您的 ID 不匹配。 (place_autocomplete_fragment1)
像这样使用getActivity()
。
PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);
片段id应该是android:id="@+id/place_autocomplete_fragment1"
<fragment
android:id="@+id/place_autocomplete_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"/>
此错误的另一个合理原因是您必须将 Fragment
class 从 android.support.v4.app.Fragment
扩展到 PlaceAutoCompleteFragment
,从而扩展 Fragment
class 来自 com.google.android.gms.location.PlaceAutocompleteFragment
。
为避免这种情况,您 should/can 使用 SupportPlaceAutoCompleteFragment
而不是您正在使用。
SupportPlaceAutocompleteFragment autocompleteFragment1 = (SupportPlaceAutocompleteFragment)
getActivity.getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);
使用此功能,您的自动完成小部件也可以在较低版本的设备上使用。
- 在您的布局 xml 中更改 SupportPlaceAutocompleteFragment 而不是
fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
更改 getChildFragmentManager() 而不是
getFragmentManager()并使用 SupportPlaceAutocompleteFragment 而不是PlaceAutocompleteFragment在你的 java 文件中,.SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
这是 Kotlin 中的解决方案。这专门用于如果您想在片段中启动自动完成片段。
val autocompleteFragment = childFragmentManager.findFragmentById(R.id.autocomplete_support_fragment) as AutocompleteSupportFragment?
<fragment
android:id="@+id/autocomplete_support_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>