ViewTreeObserver NullPointerException Crash in Android

1k views Asked by At

I am getting below viewTreeObserver crash on firebase. What could be the possible cause? ViewTreeObserver is used at multiple places in the app so unsure where exactly this crash is happening

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.ViewTreeObserver$OnGlobalLayoutListener.onGlobalLayout()' on a null object reference
       at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1056)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3050)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2075)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8471)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1065)
       at android.view.Choreographer.doCallbacks(Choreographer.java:889)
       at android.view.Choreographer.doFrame(Choreographer.java:816)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1050)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7824)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:512)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1027)
1

There are 1 answers

0
Diego Torres Milano On BEST ANSWER

If an application keeps a long-lived reference to a ViewTreeObserver, it should always check for the result of isAlive() before calling any other method.

See https://developer.android.com/reference/android/view/ViewTreeObserver#isAlive()