Getting “Fragment did not create a view” after addition of other Fragment without UI

后端 未结 2 1059
南旧
南旧 2021-01-04 08:14

I\'m having a really weird problem. I have a common fragment that is added in almost every activity of my app. This fragment shows a small version of the player bar. So it l

2条回答
  •  旧巷少年郎
    2021-01-04 08:44

    For future reference, I found that my issue lay in my fragment's onCreateView() method. I had the following:

    private lateinit var containingView : View
    
    // ......
    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        containingView = inflater.inflate(R.layout.my_fragment_layout, container, false)
        return view
    }
    

    .....which caused an error because while I was inflating the layout to containingView, I was actually returning view (which just happens to be a property of the fragment class of the correct type). The corrected code is this:

    private lateinit var containingView : View
    
    // ......
    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        containingView = inflater.inflate(R.layout.my_fragment_layout, container, false)
        return containingView
    }
    

提交回复
热议问题