推断类型是片段!但预计PlaceAutocompleteFragment

时间:2017-07-16 04:48:35

标签: android kotlin google-places-api

.xml:

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

代码:

val autocompleteFragment : PlaceAutocompleteFragment = paf

错误:找到的类型是片段! ,必需的是PlaceAutocompleteFragment
我已经尝试过SupportPlaceAutocompleteFragment和.getActivity()
我该怎么办?

2 个答案:

答案 0 :(得分:0)

默认情况下paf被充气时,从XML中检索到它时,它是Fragment类型,您需要将其强制转换为PlaceAutocompleteFragment

val autocompleteFragment : PlaceAutocompleteFragment = paf as PlaceAutocompleteFragment

答案 1 :(得分:0)

这有效:

val autocompleteFragment = getFragmentManager().findFragmentById(R.id.paf) as PlaceAutocompleteFragment