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

旧城冷巷雨未停 提交于 2019-11-30 13:00:56

Oh my God, I can't beleive it. I solved my problem just setting the ID of the fragment that was having problem.

Now, the XML's layout of the Activity is like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <fragment
        android:name="com.soongz.ui.fragment.ListaDeMusicasFragment"
        android:id="@+id/lista_de_musicas_fragment"
        style="?layoutListViewMusicas" />

    <fragment
        android:id="@+id/player_reduzido_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:name="com.soongz.ui.fragment.PlayerReduzidoFragment"/>
</LinearLayout>

I don't know why. It must be a bug.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!