将PlaceAutocompleteFragment添加到片段会引发错误 [英] Adding PlaceAutocompleteFragment to fragment throws error

查看:85
本文介绍了将PlaceAutocompleteFragment添加到片段会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在活动中实现PlaceAutocompleteFragment,并且工作正常.但是如何在android中的片段中实现相同的功能呢? 我已经实现了placeautocomplete片段

I have implemented PlaceAutocompleteFragment in activity and is working successfuly. But how to implement the same in fragment in android? I have implemented placeautocomplete fragment like this

 PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);

我遇到的错误是

难以发现的类型;无法将"android.support.v4.app.Fragment"转换为com.google.android.gms.location.PlaceAutocompleteFragment'.

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>

预先感谢

推荐答案

以这种方式使用getActivity().

PlaceAutocompleteFragment autocompleteFragment1  = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);

这篇关于将PlaceAutocompleteFragment添加到片段会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆