2

here's my code:

public MyPresentation(Context outerContext, Display display) {
    super(outerContext, display);

    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    View rootView = getWindow().getDecorView().getRootView();

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.x=300;
    lp.y=300;
    lp.width=lp.height=500;
    wm.updateViewLayout(rootView, lp);
}

As you can see, I'm trying to change params of my presentation through window manager.But got exception bellow:

Java.lang.IllegalArgumentException:View=com.android.internal.policy.impl.PhoneWindow$DecorView{164f32fc V.E..... R.....ID 0,0-0,0} not attached to window manager
    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:466)
    at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:322)
    at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:91)
    at com.evideostb.cbb.diveintotv.MyPresentation$override.init$body(MyPresentation.java:30)
    at com.evideostb.cbb.diveintotv.MyPresentation$override.access$dispatch(MyPresentation.java)
    at com.evideostb.cbb.diveintotv.MyPresentation.<init>(MyPresentation.java:0)

So how can I get the root view of presentation which attach to window manager?

CompileLife
  • 163
  • 1
  • 13

2 Answers2

1

As the exception name says IllegalArgumentException, DecorView not attached to window manager.
Probably Activity/Fragment is after is onPause state (no active UI) when you call getRootView().

You can add this code before you perform any action on the view:
Activity

if (isFinishing()){
//You code here
}

Fragment

if (isAdded()){
//You code here
}

Also look here maybe you suffered from common bug.

Community
  • 1
  • 1
Nir Duan
  • 6,164
  • 4
  • 24
  • 38
0

Thanks for @Nir Duan's answer. I think my problem is perform my action in wrong place.

In presentation's constructor, it's root view hasn't been add to window manager yet.

After presentation shown, perform wm.updateViewLayout(rootView, lp); would take effect.

CompileLife
  • 163
  • 1
  • 13