1

我有一个片段,它是 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)
}
4

1 回答 1

1

尝试手动将 mapFragment 添加到 FrameLayout 而不是使用<fragment标签,但前提是if(savedInstanceState == null) {通过标签获取它

于 2020-01-29T16:56:02.150 回答