Search code examples
androidandroid-dialogfragment

getActivity() return null after returning from a dialogFragment


I'm currently experiencing some troubles with DialogFragment.

My application look like this :

A MainActivity using ActionBarSherlock to display 6 tabs ( wich all contains a fragment ) On one of those tabs, I show a DialogFragment when the user click a button. This dialog contains some EditText and a button wich save the user input (A postal address) and call a listener in the MainActivity for computing.

My problem is when I turn screen with the dialog up, I managed to make him stay on the screen without losing data but when I call for the listener and then dismiss the dialog, a method is called in the fragment to add the new postal address to a list which is then mapped on a ListView and in this method getActivity() returns null.

I guess the fragment is not yet attached and/or created since my breakpoint in onResume() never break the execution. But i can't see a way to make sure my fragment is created when the listener call for the list update.

I'll be happy to provide further informations if you have any idea that can help me here, Google didn't helped much this time :(


Solution

  • Hey I had the same problem yesterday. Check out this thread for two fixes :

    DialogFragment causing nullpointer crash