admin管理员组

文章数量:1123509

I'm experiencing crashes in my Compose-based app when cleaning up AndroidView components that contain third-party ad views. The crash occurs during view detachment:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchDetachedFromWindow()' on a null object reference at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4219) at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:6108) at androidxpose.ui.viewinterop.AndroidViewHolder.onDeactivate(AndroidViewHolder.android.kt:222) at androidxpose.ui.node.LayoutNode.onDeactivate(LayoutNode.kt:1350) at androidxpose.runtime.CompositionImpl$RememberEventDispatcher.dispatchRememberObservers(Composition.kt:1283) at androidxpose.runtime.CompositionImpl.applyChangesInLocked(Composition.kt:984) at androidxpose.runtime.CompositionImpl.applyChanges(Composition.kt:1005) at androidxpose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:639) at androidxpose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:551) at androidxpose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:41) at androidxpose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109) at androidxpose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41) at androidxpose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1688) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1699) at android.view.Choreographer.doCallbacks(Choreographer.java:1154) at android.view.Choreographer.doFrame(Choreographer.java:1070) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1647) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.app.ActivityThread.main(ActivityThread.java:9063) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:588) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

The crash tends to occur when rapidly navigating between screens or when switching between tabs in a HorizontalPager that contains third-party ad views (integrated via AndroidView). Unfortunately, the stack trace doesn’t provide enough detail, and I can’t reproduce the crash on my own devices or Genymotion emulators.

How should I properly clean up AndroidView components containing third-party ad views in Compose? How can I ensure a proper view detachment when navigating or switching pages, so I avoid this NullPointerException in dispatchDetachedFromWindow()? Is there a known lifecycle or race condition related to AndroidView that might trigger this crash?

本文标签: Android Compose NullPointerException during AndroidView cleanupStack Overflow