Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActivity() return null after returning from a dialogFragment [duplicate]

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 :(

like image 430
Furzel Avatar asked Oct 07 '22 03:10

Furzel


People also ask

Can getActivity return null?

getApplicationContext() error Android Assuming the fragment is attached to an activity so getActivity() will return a non-null object, which is usually true.

How to show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


1 Answers

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

DialogFragment causing nullpointer crash

like image 68
Slickelito Avatar answered Oct 13 '22 10:10

Slickelito