NullPointerException: Attempt to read from field &

2020-02-13 23:05发布

Having some interesting issue with transaction on fragments on Android Lollipop ONLY. Crash happens when i go back and remove previously added fragment.

Here is 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 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`m receiving the crash on last code fragment.

UPDATE

After i had removed it has no crashes anymore:

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>

5条回答
别忘想泡老子
2楼-- · 2020-02-13 23:23

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 !

查看更多
劫难
3楼-- · 2020-02-13 23:30

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.

查看更多
我想做一个坏孩纸
4楼-- · 2020-02-13 23:34

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.

查看更多
家丑人穷心不美
5楼-- · 2020-02-13 23:41

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.

查看更多
叛逆
6楼-- · 2020-02-13 23:43

I tried to remove null from fragment manager.

This was the actual code:

broadcastFragment = null;
getSupportFragmentManager().beginTransaction().remove(broadcastFragment).commit();
查看更多
登录 后发表回答