Views added to a FragmentContainerView must be associated with a Fragment. View

440 阅读1分钟

将inflate的attchToRoot改为false

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return layoutInflater.inflate(R.layout.fragment_home,container,false)
}

原因是:如果fragment是通过FraggmentContainerView方式包裹的,那么fragment的inflater有一个factory2,这里的 container不是null而是一个FragmentContainerView,由于attchToRoot为true,会调用container.addview方法,这个方法里面由于inflate的这个view没有设置他指定的tag所以会报错。若不是通过FraggmentContainerView方式包裹的,container则是null。