mosby: Mosby 3.1.0 NPEs in Activity#onDestroy()

After upgrading app to Mosby 3.1.0 crash analytics have started displaying crashes like:

java.lang.RuntimeException: Unable to destroy activity {p/p.a}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.hannesdorfmann.mosby3.mvp.MvpPresenter.detachView()' on a null object reference
	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4299)
	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4317)
	at android.app.ActivityThread.-wrap6(ActivityThread.java)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1569)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:241)
	at android.app.ActivityThread.main(ActivityThread.java:6274)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.hannesdorfmann.mosby3.mvp.MvpPresenter.detachView()' on a null object reference
	at com.hannesdorfmann.mosby3.mvp.delegate.ViewGroupMvpDelegateImpl.detachPresenterIfNotDoneYet(ViewGroupMvpDelegateImpl.java:301)
	at com.hannesdorfmann.mosby3.mvp.delegate.ViewGroupMvpDelegateImpl.onActivityDestroyed(ViewGroupMvpDelegateImpl.java:255)
	at android.app.Application.dispatchActivityDestroyed(Application.java:253)
	at android.app.Activity.onDestroy(Activity.java:1851)
	at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:358)
	at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:209)
	at p.a.onDestroy(a.java:)
	at android.app.Activity.performDestroy(Activity.java:6922)
	at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1154)
	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4286)
	... 9 more

Do not have the specific repro steps available yet, but reading the source in ViewGroupMvpDelegateImpl:

  private void destroyPresenterIfnotDoneYet() {
    if (!presenterDestroeyed) {
      P presenter = delegateCallback.getPresenter();
      presenter.destroy();

To me this looks like the code is assuming too much about the fragment/activity lifecycle - the fragment lifecycle has not reached MvpFragment#onCreate() where the presenter is created but the the activity gets destroyed for some other reason, and presenterDestroeyed defaults to false.

For what it’s worth, there’s also a typo in presenterDestroeyed.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Working on it, along migrating to android x