【问题标题】:Why Fragment SupportMapFragment has not been attached yet为什么 Fragment SupportMapFragment 还没有附加
【发布时间】:2021-11-30 06:25:21
【问题描述】:

我正在尝试在我的片段上实现 supportmapfragment,但由于某种原因它给了我错误。我做错了什么?

我得到的错误:java.lang.IllegalStateException: Fragment SupportMapFragment{67446a6} (09078183-572b-41c6-8795-7441157371d5) 尚未附加。

supportMapFragment.getMapAsync(this)。在此之前从未被称为崩溃

class MapFragment : Fragment(), OnMapReadyCallback {

private lateinit var googleMap: GoogleMap

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View {
    val view = inflater.inflate(R.layout.fragment_map, container, false)

    val supportMapFragment = SupportMapFragment()
    supportMapFragment.childFragmentManager.findFragmentById(R.id.google_map)
    supportMapFragment.getMapAsync(this)

    return view
   }
}

fragment_map .xml 文件看起来像:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primaryBackground"
    xmlns:app="http://schemas.android.com/apk/res-auto">


       <androidx.fragment.app.FragmentContainerView
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/google_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: google-maps kotlin android-fragments supportmapfragment


    【解决方案1】:

    添加:

    private lateinit var mapFragment: SupportMapFragment
    

    删除

    val supportMapFragment = SupportMapFragment()
    

    变化

    supportMapFragment.childFragmentManager.findFragmentById(R.id.google_map)
    

    mapFragment = childFragmentManager.findFragmentById(R.id.google_map) as SupportMapFragment
    

    解决这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2017-11-29
      • 1970-01-01
      • 2018-12-13
      • 2021-03-01
      相关资源
      最近更新 更多