Throwed a NullPointerException: The fragment has been removed from FragmentManager in testing

Viewed 141
@Test
    fun testBottomSheetShown(){
        val args = Bundle().apply {
            putString("EXTRA_TITLE", "title")
            putString("EXTRA_CURRENT_VALUE", "choiceOne")
        }
        val scenario = launchFragmentInContainer<ChoicesBottomSheet>(args, R.style.BottomSheetDialog)
        scenario.onFragment { 
            assertEquals(Lifecycle.State.RESUMED, it.lifecycle.currentState)
        }
    }

I tried to test my Bottomsheet with fragment scenario but it throws the error below:

java.lang.NullPointerException: The fragment has been removed from FragmentManager already.

1 Answers

launchFragmentInContainer will cause the ChoicesBottomSheet to reach the RESUME state.

The fragment has been removed from FragmentManager already

The above error is thrown if the Fragment is already destroyed, and you are trying to access it. So I believe that your BottomSheet fragment is dismissed during its flow to RESUME state. Please check if you have a dismiss() call somewhere in your BottomSheet code, that is getting called before your fragment reaches the onResume() call.

Related