[英]Type mismatch. Required: Fragment, Found: PlaceAutocompleteFragment
I bumped into a 我碰到一个
Duplicate ID, tag null, or parent id with another fragment for placeAutocompleteFragment 重复的ID,标签null或父ID,以及placeAutocompleteFragment的另一个片段
error, and so i followed this solution to try and fix it by adding the placeAutocompleteFragment via a fragment transaction using the childFragmentManager
误差,所以我也跟着此溶液 ,试图通过经由使用一个片段交易添加placeAutocompleteFragment修复它childFragmentManager
fragment class onCreateView
片段类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 .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>
The problem now is on the line 现在的问题就在网上
fm.beginTransaction().add(R.id.address_layout, placeAutocompleteFragment, "placeAutocompleteFragment").commit()
I get a type mistake for placeAutocompleteFragment
Type mismatch. 我收到placeAutocompleteFragment
类型不匹配的类型错误。 Required: Fragment, Found: PlaceAutocompleteFragment? 必需:片段,找到:PlaceAutocompleteFragment?
Any suggestions? 有什么建议么?
Thanks 谢谢
Problem 问题
getChildFragmentManager will return support library version of FragmentManager. getChildFragmentManager将返回FragmentManager的支持库版本。 So you should pass fragment object which inherits support library version of Fragment. 因此,您应该传递继承Fragment支持库版本的fragment对象。
But PlaceAutocompleteFragment inherits android.app.Fragment. 但是PlaceAutocompleteFragment继承了android.app.Fragment。
Solution 解
Using SupportPlaceAutocompleteFragment instead PlaceAutocompleteFragment. 使用SupportPlaceAutocompleteFragment代替PlaceAutocompleteFragment。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.