Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After migrating to AndroidX application crashes with Attempt to invoke androidx.fragment.app.FragmentManagerImpl.isDestroyed() on a null ref

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();
            }
        };
like image 225
Mircea Slimu Avatar asked Jun 16 '19 11:06

Mircea Slimu


People also ask

How to migrate an existing Android project to androidx?

With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar. The refactor command makes use of two flags.

What versions of androidx libraries are supported by the migration tool?

The migration tool pulls in the most recent release of the AndroidX libraries. As a result, you may end up with an alpha version of some libraries rather than the stable version. For example, the support library version before migration might be: And after migration, you would be using the alpha version of the corresponding AndroidX library:

Can I get an old project to run on Android X?

Due to the expanding number of packages that require Android X (namely Xamarin.Forms 5) it is essential that you can get an old project compiling and running on Android X. This can come as quite a challenge especially to massive projects that are quite old.

How to migrate from jetpack compose to androidx?

So, for example, to take advantage of Jetpack Compose or CameraX, you need to migrate to the AndroidX namespace. back up your project. If you’re using source control tools, it is still recommended that you make a backup, because migration will change many of the files in your project. create a new branch on which to make the migration changes.


1 Answers

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.

like image 196
Mircea Slimu Avatar answered Sep 27 '22 18:09

Mircea Slimu