13

The full stack includes only android core code:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()' on a null object reference
    at androidx.fragment.app.Fragment.performDetach(Fragment.java:2844)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1033)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1237)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2075)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1865)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1820)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1726)
    at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

It happens when replacing the fragment in main activity:

Runnable mPendingRunnable = new Runnable() {
            @Override
            public void run() {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frame, fragment);
                fragmentTransaction.commitAllowingStateLoss();
            }
        };
Mircea Slimu
  • 651
  • 5
  • 10

4 Answers4

42

I found a piece of code in myFragment.onDetach that was causing this:

It was a workaround from Getting the error "Java.lang.IllegalStateException Activity has been destroyed" when using tabs with ViewPager

        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

Now, in androidx, this workaround is not needed.

Mircea Slimu
  • 651
  • 5
  • 10
5

In my module's gradle, I fixed it by reverting

implementation 'androidx.appcompat:appcompat:1.1.0-beta01'

to

implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'

(The above answer with the childFragmentManager stuff did not help.)

S Fitz
  • 1,074
  • 15
  • 31
  • 2
    I have filed an Issue for the AppCompat library here: https://issuetracker.google.com/issues/136741838 – mvbrenes Jul 05 '19 at 20:52
  • ur solution worked..but do you know what caused this problem? – justArandomUser Feb 01 '20 at 08:47
  • I ran into this, it looks like `androidx.appcompat:1.0.0` does not have this problem, but `androidx.appcompat:1.1.0` split out the Fragment implementation to the `androidx.fragment` library, and this problem shows up sometime after that. – dragonx Oct 13 '22 at 18:52
2

I was straggling last one days about this issue and finally i found the solution. Here i am using gradle

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

and i replaced my fragment using this code.

 getActivity().getSupportFragmentManager().beginTransaction().detach(new    KiKorbo()).replace(R.id.calendar1, caldroidFragment).attach(caldroidFragment)
            .addToBackStack(null).commit();

Here KiKorbo.java was my parent fragment and i replaced caldroidFragment.

pavel
  • 1,603
  • 22
  • 19
1

In my module's gradle, I fixed it by reverting

implementation 'androidx.appcompat:appcompat:1.2.5'

to

implementation 'androidx.appcompat:appcompat:1.1.0'
Seachal
  • 27
  • 3