【发布时间】:2020-01-29 13:30:08
【问题描述】:
我有一个片段,它是 BottomsheetDialogFragment 的子片段,在该片段内我有一个谷歌地图。起初,当我使用拱组件 NavController 打开这个片段时,它工作正常,而不是如果我关闭这个片段并尝试再次打开它,它会崩溃。 这是错误日志
Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #71 in com.example.com:layout/product_location_fragment: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #71: Duplicate id 0xffffffff, tag mapFragment, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:116)
这是片段本身
class ProductLocationFargment : BottomSheetDialogFragment, {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.product_location_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//this also always returns null
val frag = childFragmentManager.findFragmentById(R.id.mapFragment) as? SupportMapFragment
}
}
和布局xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:tag="mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="@id/leftGuideView"
app:layout_constraintEnd_toEndOf="@id/rightGuideView" />
</androidx.constraintlayout.widget.ConstraintLayout>
这就是我从另一个片段开始该片段的方式
button.setOnClickListener {
val direction = AnotherFragmentDirections.actionAnotherFragmentToProductLocationFargment()
navController.navigate(direction)
}
【问题讨论】:
-
尝试手动将 mapFragment 添加到 FrameLayout 中,而不是使用
<fragment标签,但前提是if(savedInstanceState == null) {否则通过标签获取它 -
哇,感谢工作,但如果您知道为什么它不适用于标准流程? p.s.如果您写此评论作为答案,我会接受
标签: android android-dialogfragment android-architecture-components android-architecture-navigation android-bottomsheetdialog