findFragmentById always returns null

前端 未结 9 602
时光说笑
时光说笑 2020-12-01 17:31

I\'m defining an ID for my fragment in the xml layout:




        
9条回答
  •  渐次进展
    2020-12-01 18:25

    I was using android.support.v4.app.Fragment in my layout while calling getFragmentManager() which actually searched for android.app.Fragment subclasses and I got null. So the fix was to call getSupportFragmentManager() instead.

    In general make sure the package of a fragment you are subclassing and using in your layout is the same returned by the corresponding FragmentManager which performs search.

提交回复
热议问题