NullPointerException: Attempting to read from field 'int android.app.Fragment.mContainerId' in null object reference

You have an interesting issue with chunk transaction on Android Lollipop ONLY. The crash occurs when I go back and delete the previously added fragment.

Here is the stacktrace:

FATAL EXCEPTION: main
Process: com.parkme.consumer, PID: 15560
    java.lang.NullPointerException: Attempt to read from field 'int android.app.Fragment.mContainerId' on a null object reference
    at android.app.BackStackRecord$1.onPreDraw(BackStackRecord.java:1131)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1970)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

      

How I am doing the transaction:

getFragmentManager().beginTransaction()
            .replace(R.id.list_holder, listFragment)
            .commit();

      

R.id.list_holder

<FrameLayout
        android:id="@+id/list_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</FrameLayout>

      

How I remove it:

 getFragmentManager().beginTransaction()
            .remove(listFragment)
            .commit();

      

And I get the crash of the last piece of code.

UPDATE

After I removed it, no more crashes:

Transition slideTransition = TransitionInflater.from(ParkmeApplication.getContext()).inflateTransition(R.transition.slide_right);
setExitTransition(slideTransition);

      

slide_right:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <slide
        android:duration="@android:integer/config_shortAnimTime"
        android:slideEdge="right"/>
</transitionSet>

      

+3


source to share


5 answers


The deal was to not use the method getFragmentManager()

and use getSupportFragmentManager()

and jump from the class instead v4.app.Fragment

. After I was removed from android.app.Fragment

, it works without a hitch.



So, my recommendation for everyone is if you have this kind of use getFragmentManager()

and have support for the libraries used in your application, then just go to the support manager. Even he works for you. Or you might crash with some weird bug like me. Thanks to everyone who took the time to give me advice!

+1


source


I was getting this error when doing a Fragment Transaction on a Fragment that does not exist in FragmentManager

:

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



The snippet with "MyTag" does not exist in FragmentManager

, so it FragmentTransaction

tries to do some work on slice zero.

+1


source


The replace function removes the previously added fragments in the R.id.list_holder file and adds them to the new fragment. Thus, when you delete a fragment, the system cannot find it because it has already been removed by the replace function.

0


source


I had a similar problem and misnamed addToBackStack()

when adding the first snippet to the layout. Removed and it works well. Obviously it runs on a higher Android level, for example O

.

0


source


I tried removing null from the fragment manager.

This was the actual code:

broadcastFragment = null;
getSupportFragmentManager().beginTransaction().remove(broadcastFragment).commit();

      

0


source







All Articles