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

后端 未结 2 1056
南旧
南旧 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:28

    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.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题