NullPointerException: Attempt to read from field 'int android.app.Fragment.mContainerId' on a null object reference

孤人 提交于 2019-11-29 13:44:12

I was getting this error while doing Fragment Transaction on Fragment which does not exist in FragmentManager:

fragmentTransaction.hide(fragmentManager.findFragmentByTag("MyTag"));

Fragment with "MyTag" does not exist in FragmentManager, so FragmentTransaction tries to do job on null Fragment.

The deal was in to do not use getFragmentManager() method and use getSupportFragmentManager() instead and extend from v4.app.Fragment class. After i have been moved out from android.app.Fragment it is working now without crashes.

So my recommendation for all if you have such uses of getFragmentManager() and you have anyone support libraries used in your app then just move to support manager. Even it`s working for you. Or you can hit the crash with some strange bug as i did. Thanks to all who got time to give me advices !

The replace function removes previously added fragments at R.id.list_holder and adds in the new fragment. So, when you remove your fragment, the system can't find it since it's already been removed by the replace function.

I've had a similar issue and I was wrongly to call addToBackStack() when adding first fragment into the layout. Have removed it and it works well. Obvious is that it's working in higher Android level such as O.

I tried to remove null from fragment manager.

This was the actual code:

broadcastFragment = null;
getSupportFragmentManager().beginTransaction().remove(broadcastFragment).commit();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!