Несоответствие типов. Обязательно: фрагмент, найдено: PlaceAutocompleteFragment

Я наткнулся на

Дублированный идентификатор, нулевой тег или родительский идентификатор с другим фрагментом для placeAutocompleteFragment

ошибка, и поэтому я последовал этому решению, чтобы попытаться исправить его, добавив placeAutocompleteFragment через транзакцию фрагмента, используя childFragmentManager

класс фрагмента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 ответ

Решение

проблема

getChildFragmentManager возвращает версию библиотеки поддержки FragmentManager. Таким образом, вы должны передать фрагмент объекта, который наследует версию библиотеки поддержки фрагмента.

Но PlaceAutocompleteFragment наследует android.app.Fragment.

Решение

Использование SupportPlaceAutocompleteFragment вместо PlaceAutocompleteFragment.

Другие вопросы по тегам