Swapnil Deshmukh
Swapnil Deshmukh

Reputation: 665

WindowManager null

I written separate class of adapter for gridview present in fragment. If a called windowmanger first time from adapter constructor it worked but if i changed orientation windowmanager becomes null.

I written code for getting window manager like

public CalendarAdapter(Context c, Calendar monthCalendar) {
        getWindowDimension();
    }
void getWindowDimension() {

        Display display = (((Activity)mContext).getWindowManager().getDefaultDisplay());
        windowHeight = display.getHeight();
        windowWidth = display.getWidth();

    }

Even tried with getSystemService(WINDOW_SERVICE)

I think problem is because of activity recreation. Its not getting window of the activity if its not created. Throws Nullpointer exception for window manager.

In manifest activity has this flags :

android:configChanges="orientation|screenLayout|navigation|layoutDirection"

Please help for same.

Upvotes: 1

Views: 1398

Answers (1)

C B J
C B J

Reputation: 1868

My guess is that when you are calling mContext.getwindowManager() after rotation mContext hasn't yet been attached to a window, see Activity.onAttachedToWindow. If you are calling it from inside a fragment, you will be wanting to ensure you aren't calling it until onFragmentAttached.

In short, you probably want to set up your adapter a little bit later in your fragment life-cycle.

Upvotes: 1

Related Questions