0

I'm trying to understand how it is possible. I have a crash which happens occasioanlly. Here is the stacktrace:

java.lang.NullPointerException: Attempt to invoke interface method 'com.mycompany.myapp.fragments.timeline.TimelineFragmentView com.mycompany.myapp.injections.FragmentComponent.createTimelineFragmentView()' on a null object reference
    at com.mycompany.myapp.fragments.timeline.TimelineFragment.createView(TimelineFragment.java:28)
    at com.mycompany.myapp.fragments.timeline.TimelineFragment.createView(TimelineFragment.java:17)
    at com.mycompany.myapp.fragments.BaseFragment.onCreateView(BaseFragment.java:33)
    at com.mycompany.myapp.fragments.ScreenFlowBaseMenuFragment.onCreateView(ScreenFlowBaseMenuFragment.java:22)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:958)
    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1666)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586)
    at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:169)
    at com.mycomany.mayapp.MainActivity.onBackPressed(MainActivity.java:125)
    at android.app.Activity.onKeyUp(Activity.java:2483)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2736)
    at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
    at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:224)

    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)

And FragmentComponent is nulled in the last line of the next method:

@Override
protected void onDestroy() {
    super.onDestroy();

    backendResponseMonitor.stop();
    view.stop();

    Graph graph = (Graph) getApplication();
    graph.destroyFragmentComponent();
}

Since everything runs on UI thread then it looks like back button processing is performed after activity onDestroy called. Am I missing some concept of activity lifecycle?

Don't keep activities developer option flag is turned on

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • 3
    try with moving `super.onDestroy()` to be the last statment in the method. – JafarKhQ Mar 14 '16 at 10:59
  • Thanks! What is the cause of the crash? How moving the super call will help with crash? – Eugen Martynov Mar 14 '16 at 11:00
  • `super.onDestroy();` will cleanup the `Activity`. check this answer http://stackoverflow.com/questions/9625920/should-the-call-to-the-superclass-method-be-the-first-statement/9626268#9626268 – JafarKhQ Mar 14 '16 at 11:03
  • Hense I agree with the comment about changing the order of calling super. I still can not understand what happens. My activity `onDestory` completely executed before `keyCode` is processed. So I don't see much difference to have in the start or end of the method – Eugen Martynov Mar 14 '16 at 11:09
  • Look like you have an issue with the app flow, somehow `TimelineFragment.createView` is called on `Destroying` the `Fragment`s. do you keep a references to your `Fragment`s in the `AppContext` ? – JafarKhQ Mar 14 '16 at 11:21
  • I don't know for sure about leaks. The stacktrace starts from Android OS, so Android is invoking back button processing after activity should be destroyed – Eugen Martynov Mar 14 '16 at 11:28

1 Answers1

0

super.onDestroy(); calls the super class onDestroy method that clears all the references used in your activity. Hence you are getting null point exception. Just make your onDestroy as follows.

@Override
protected void onDestroy() {
    backendResponseMonitor.stop();
    view.stop();

    Graph graph = (Graph) getApplication();
    graph.destroyFragmentComponent();

    super.onDestroy();
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
  • 1
    this is just another guy's comment you turned into your answer. not cool man :P – Vucko Mar 14 '16 at 11:06
  • Am sorry man i didnt notice it. Started typing and then went off for a bit. Sorry Vucko and sorry JafarKhQ – Ragesh Ramesh Mar 14 '16 at 11:08
  • I know bro, happens to me a lot also, I just open the post and forget about it for 5 min, when I'm back -> already answered and I hadn't noticed :D – Vucko Mar 14 '16 at 11:09
  • It is not super that clears references. It is `graph.destoryFragmentComponent()` method – Eugen Martynov Mar 14 '16 at 11:10