Error inflating Fragment in Dialog the second time

前端 未结 6 882
深忆病人
深忆病人 2020-12-03 08:31

I have the following code in an Activity that starts a dialog for a layout that contains a fragment.

...
case R.id.pick_resource:
        dialog = new Dialo         


        
6条回答
  •  温柔的废话
    2020-12-03 08:57

    Does the fragment in your layout have an android:id attribute?

    I suspect this is because the fragment is instantiated each time your layout is inflated, the first time the ID isn't being used, but the second time the FragmentManager still thinks your Fragment is alive, so the ID is considered a duplicate.

    Try removing the android:id attribute from your fragment if it exists, or add a placeholder layout such as a framelayout and use a fragmenttransaction to dynamically add the fragment each time your dialog is created.

提交回复
热议问题