类型不匹配。必需:片段,找到:PlaceAutocompleteFragment

时间:2019-02-21 02:54:58

标签: android android-fragments kotlin placeautocompletefragment child-fragment

我碰到了

  

重复的ID,标记为null或父ID,以及另一个用于   placeAutocompleteFragment

错误,因此我遵循此solution尝试通过使用childFragmentManager通过片段事务添加placeAutocompleteFragment来修复它

片段类onCreateView

val fm: FragmentManager = childFragmentManager
var placeAutocompleteFragment: PlaceAutocompleteFragment? = fm.findFragmentByTag("placeAutocompleteFragment") as PlaceAutocompleteFragment?

if (placeAutocompleteFragment == null){
    placeAutocompleteFragment = PlaceAutocompleteFragment()
    fm.beginTransaction().add(R.id.address_layout, placeAutocompleteFragment, "placeAutocompleteFragment").commit()
}

.xml

<android.support.constraint.ConstraintLayout
...
...
...
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/address_layout"
        android:background="@drawable/rounded_search_bar"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
        android:orientation="horizontal">
    </LinearLayout>
...
...
...
</android.support.constraint.ConstraintLayout>

现在的问题在线了

fm.beginTransaction().add(R.id.address_layout, placeAutocompleteFragment, "placeAutocompleteFragment").commit()

我遇到placeAutocompleteFragment类型不匹配的类型错误。需要:片段,找到:PlaceAutocompleteFragment?

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:2)

问题

getChildFragmentManager将返回FragmentManager的支持库版本。因此,您应该传递继承自Fragment支持库版本的fragment对象。

但是PlaceAutocompleteFragment继承了android.app.Fragment。

解决方案

使用SupportPlaceAutocompleteFragment代替PlaceAutocompleteFragment。