1

when I start the app the first time the code below works just fine. But when leaving the app and opening it again I get an error saying getActivity() returns null.

I'm doing this code in a Fragment:

(getActivity()).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    enableMenu();
                    openMenu();
                    navigateToFragment(new BlankFragment());
                }
            });

What to do ?

How can I get the Activity ?

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
Christer
  • 2,746
  • 3
  • 31
  • 50
  • 1
    did you search SO, try this http://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function – Hughzi Dec 02 '15 at 09:39

2 Answers2

5

Create object of Activity and assign that on the onAttach Method like below. Some times getActivity gives null so its a better way to make activity instance in onAttach and use that instance.

private Activity mActivity;

@Override
public void onAttach(Activity activity) {
   super.onAttach(activity);
   mActivity = activity;
}

Now use this object instead of the getActivity()

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Sandy
  • 985
  • 5
  • 13
  • this answer doesn't make sens ... `mActivity` will be null if he would call his code at the same time/place as where is now ... obviously he just need to move `getActivity()` call to the place/time in Fragment lifecycle where/when it would not return null ... – Selvin Dec 02 '15 at 10:19
  • @Selvin whats happns here is when your fragment calls first time it is attached to the activity so it working fine but when fragment is detached its `getActivity` also going null as fragment is detached so when you later come to the same fragment that fragment `getActivity` is returns null but as fragment is detached and now fragments onAttached method will call so you can get the new actvity object from there. and it works like charm!!! – Sandy Dec 02 '15 at 10:33
  • 2
    *when fragment is detached* <= that's the key ... **then you should not use parent(no longer) Activity in the fragment** ... *and it works like charm!!!* and may stop working in some new version of android as you don't take a fragment's lifecycle in the account ... that's how bad programs are made – Selvin Dec 02 '15 at 10:35
  • @Selvin I just say that activity will be available either in onAtttached method after that method called. Check this Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null. wrriten on the developer blog http://developer.android.com/guide/components/fragments.html If you have any proper solution then it will be appriciated please – Sandy Dec 02 '15 at 11:07
  • @Selvin : I know it's pretty old post, but how can I "not use" parent activity in fragment if I want to start a new activity? In this case, I got my NPE in my call new Intent(getActivity(), newActivity.class); – gamerounet Feb 03 '17 at 14:47
1

The method onAttach(Activity activity) is now deprecated. You should use this one:

@Override
public void onAttach(Context context) {
super.onAttach(context);
activity = getActivity();
}