android – Odd logcat error when switching to new fragment-ThrowExceptions

Exception or error:

I haven’t encountered a stack trace such as this. It’s particularly weird because I can only get the error to be thrown when debugging. (Running the app without debugging does not yield this error). This happens upon selecting a particular page from my navigation drawer. I have just recently switched from activities to fragments and I may not have handled my fragment transactions correctly. I would appreciate any input! Thanks a lot guys, happy holidays!

Here’s the stack trace:

Process: com.kohlerbear.whowascnscalc, PID: 2415
java.lang.NullPointerException: Attempt to read from field 'boolean android.support.v4.app.BackStackRecord.mAddToBackStack' on a null object reference
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:685)
        at android.support.v4.app.FragmentManagerImpl.execPeerrorndingActions(FragmentManager.java:1479)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
        at android.os.Handler.handleCallback(Handler.java:740)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5223)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

And here is how I am handling my fragment change (Not sure if this is the problem, just judging by the fact that the trace mentions the backstack). The app crashes (again, only when debugging) regardless of whether or not I call ft.addToBackStack(null) .

Fragment frag = new ThirdScreenFragment(); 
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, frag);
ft.addToBackStack(null);
ft.commit();`

Any ideas and input are appreciated 🙂

How to solve:

I found a workaround for this issue. Put a breakpoint in the android.support.v4.app.BackStackRecord.run() method and set break condition to false.

enter image description here

###

I recently ran into the same error message in Android studio. I could run my application but once I tried to start debugging, I would receive “Attempt to read from field ‘boolean android.support.v4.app.BackStackRecord.mAddToBackStack’ on a null object reference”. The solution was to select in Android Studio, Run (toolbar) -> View Break Points -> disable each break point one by one until debugging started to work again. Doing this doesn’t remove the break point entirely it simply disables them, so there’s no need to worry that you’d need to remove all your break points to find the one causing the issue. It seems that one of the break points I had setup in a IntentService class was causing the issue. I’m not sure why it was causing an issue, but it seemed to fix this error for me.

Leave a Reply

Your email address will not be published. Required fields are marked *