bluelinelabs / Conductor

A small, yet full-featured framework that allows building View-based Android applications
Apache License 2.0
3.9k stars 343 forks source link

Crash when executing transaction on child router during activity recreation #649

Closed Tom-Alphero closed 3 years ago

Tom-Alphero commented 3 years ago

I've been getting a few reports in production of this and have been able to reproduce it. I'm not 100% of the cause but when executing a transaction on a Router, it crashes as its container field is null after being cleared at some point, I assume in ControllerHostedRouter.removeHost()? It could be due to the interactions between LifecycleController, LiveData and ViewModel, idk.

Sample app: ReproConductorCrash.zip

To reproduce:

  1. Launch app
  2. Change orientation, or go to split screen, or otherwise cause activity recreation
  3. App will crash with the following in log cat:
2021-07-01 07:33:51.030 21449-21449/com.example.conductorcrash D/MainController: Transacting: controllerHash=6528016, activityHash=127379198, page=page1

2021-07-01 07:33:57.548 21449-21449/com.example.conductorcrash D/MainController: Transacting: controllerHash=6528016, activityHash=170978170, page=page1

2021-07-01 07:33:57.551 21449-21449/com.example.conductorcrash E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.conductorcrash, PID: 21449
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.conductorcrash/com.example.conductorcrash.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.ViewGroup.post(java.lang.Runnable)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
        at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5802)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5710)
        at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.ViewGroup.post(java.lang.Runnable)' on a null object reference
        at com.bluelinelabs.conductor.Router.performControllerChange(Router.java:840)
        at com.bluelinelabs.conductor.Router.performControllerChange(Router.java:807)
        at com.bluelinelabs.conductor.Router.performControllerChange(Router.java:785)
        at com.bluelinelabs.conductor.ControllerHostedRouter.performControllerChange(ControllerHostedRouter.java:118)
        at com.bluelinelabs.conductor.Router.replaceTopController(Router.java:219)
        at com.example.conductorcrash.MainController.onViewCreated$lambda-1(MainController.kt:41)
       at com.example.conductorcrash.MainController.lambda$scBHRVvpoBhkbtRxwkuk9d756Tk(Unknown Source:0)
        at com.example.conductorcrash.-$$Lambda$MainController$scBHRVvpoBhkbtRxwkuk9d756Tk.onChanged(Unknown Source:4)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:146)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:468)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:425)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at com.bluelinelabs.conductor.archlifecycle.ControllerLifecycleOwner$1.postCreateView(ControllerLifecycleOwner.java:29)
        at com.bluelinelabs.conductor.Controller.inflate(Controller.java:1069)
        at com.bluelinelabs.conductor.ControllerChangeHandler.executeChange(ControllerChangeHandler.java:195)
        at com.bluelinelabs.conductor.ControllerChangeHandler.executeChange(ControllerChangeHandler.java:159)
        at com.bluelinelabs.conductor.Router.performControllerChange(Router.java:847)
        at com.bluelinelabs.conductor.Router.performControllerChange(Router.java:807)
        at com.bluelinelabs.conductor.Router.rebindIfNeeded(Router.java:553)
        at com.bluelinelabs.conductor.Conductor.attachRouter(Conductor.kt:30)
        at com.example.conductorcrash.MainActivity.onCreate(MainActivity.kt:19)
        at android.app.Activity.performCreate(Activity.java:7963)
        at android.app.Activity.performCreate(Activity.java:7952)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)

Let me know if more info needed!

Tom-Alphero commented 3 years ago

Now that I've been looking into it further I believe it's because I was only creating the child router if it wasn't null

if (contentRouter == null) {
    contentRouter = this.getChildRouter(view.findViewById(R.id.childChangeHandlerFrameLayout), "mainRouter")
}

Removing the if fixes the crash. Leaving up for others.