1

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.

Swapnil Deshmukh
  • 665
  • 1
  • 6
  • 23

1 Answers1

1

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.

C B J
  • 1,838
  • 16
  • 22
  • thanks james. I tried to code on window attached. As i logged all lifecycle onAttached() -> onCreateView() -> onViewCreated() is sequence. I m trying create adapter in viewCreated() first time it loads good but as i changed orientation same issue null pointer to winowManager in adapter constructor. If window not attached it will not go to onAttach() – Swapnil Deshmukh Aug 20 '13 at 19:33